开发者

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")

If there is not another solution tell me to delete this question and use my (beep) solution.


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();
    }
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜