开发者

Dynamic Increase / Decrease the number with C#

开发者 https://www.devze.com 2023-04-10 02:28 出处:网络
I have two buttons to reduce or increase the number. Also, I have 开发者_StackOverflowa label which is has value zero. How can I increase or decrease without giving zero value to the Label in C#?

I have two buttons to reduce or increase the number. Also, I have 开发者_StackOverflowa label which is has value zero. How can I increase or decrease without giving zero value to the Label in C#?

Code:

int sayi = int.Parse(lbltext1.Text);

sayi = sayi - 1;

lbltext1.Text = sayi.ToString();


Try something like this...(not tested)

void IncreaseBtn_Click(Object sender, EventArgs e)
{
    var value = this.myLabel.Text;
    var intValue = 0;
    Int32.TryParse(value, out intValue);
    this.myLabel.Text = (++intValue).ToString();
}

void DecreaseBtn_Click(Object sender, EventArgs e)
{
    var value = this.myLabel.Text;
    var intValue = 0;
    Int32.TryParse(value, out intValue);
    this.myLabel.Text = (--intValue).ToString();
}


Store it as a member variable. Then increment/decrement it. Then set the Label's Text property to the string version of the value.

0

精彩评论

暂无评论...
验证码 换一张
取 消