ChrisJJ
ChrisJJ

Reputation: 2302

How to merge change events from multiple simultaneously set controls?

I have four numericUpDowns the values of which influence an output control. Thay share a ValueChanged event handler that refreshes the text control accordingly. That's fine for user changes on the numericUpDown controls themselves. But I also have a button that sets the four numericUpDown control values in a single operation, and I want the output control refreshed once at the end, rather than four times i.e. once per set. How best to arrange this?

Upvotes: 1

Views: 121

Answers (2)

Thomas Levesque
Thomas Levesque

Reputation: 292555

Just use a flag:

private bool _updatingAllValues;

private void ValueChanged(object sender, EventArgs e)
{
    if (_updatingAllValues)
        return;

    // refresh the control...
}

private void btnUpdateAllValues_Click(object sender, EventArgs e)
{
    try
    {
        _updatingAllValues = true;
        // update all NumericUpDowns...
    }
    finally
    {
        _updatingAllValues = false;
    }

    // refresh the control...
}

Upvotes: 2

Enigmativity
Enigmativity

Reputation: 117124

You will either need to detach and reattach the event handlers during set operation or use a boolean flag that you turn on at the start of a set change and turn off at the end to indicate not to process the numeric changes.

Upvotes: 2

Related Questions