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.
精彩评论