How can I prevent the raising of Correlative events?
I have two text box and i want to calculate each text box when the other changed.but my program fall into a loop.
private b开发者_Go百科ool suspendEvents = false;
private void txtYourPercent_ValueChanged(object sender, EventArgs e)
{
if (suspendEvents)
return;
suspendEvents = true;
txtMyPercent.value = 100 - txtYourPercent.value;
suspendEvents = false;
}
private void txtMyPercent_ValueChanged(object sender, EventArgs e)
{
if (suspendEvents)
return;
suspendEvents = true;
txtYourPercent.value = 100 - txtMyPercent.value;
suspendEvents = false;
}
It's my way.This method is disgusting.Any better solution? ("WinForms")
You could assign both textboxes the same chenge event handler:
this.txtMyPercent.TextChanged += new System.EventHandler(this.txtPercent_TextChanged);
this.txtYourPercent.TextChanged += new System.EventHandler(this.txtPercent_TextChanged);
and then:
private void txtPercent_TextChanged(object sender, EventArgs e)
{
if (sender == txtMyPercent)
{
txtYourPercent.Text = (100 - int.Parse(txtMyPercent.Text)).ToString();
}
else if (sender == txtYourPercent)
{
txtMyPercent.Text = (100 - int.Parse(txtYourPercent.Text)).ToString();
}
}
精彩评论