title: How do I ignore simple events firing when changing control states in C# Windows Forms?
body: <p>I'm creating a simple form in C# Windows Forms and run into a common situation where one control can alter the state of another, but both controls' events trigger some other method.</p>
  
  <p>For example, consider a <code>Checkbox</code> and <code>NumericUpDown</code> where the state or value of either one should trigger something to redraw. The <code>NumericUpDown</code> is dependent upon the <code>Checkbox</code> meaning it could be disabled or ignored unless the <code>Checkbox</code> is checked.</p>
  
  <p>However, it is convenient for the user to change the <code>NumericUpDown</code> value, and have the <code>Checkbox</code> be automatically checked if it wasn't already.</p>
  
  <p>So here are the methods in question:</p>
  
  <pre><code>private void chkState_CheckedChanged(object sender, EventArgs e)
  {
      RedrawStuff();
  }
  
  private void numStateValue_ValueChanged(object sender, EventArgs e)
  {
      if (!chkState.Checked)
          chkState.Checked = true;
      RedrawStuff();
  }
  </code></pre>
  
  <p>The problem of course, is that changing the <code>NumericUpDown</code> value causes <code>RedrawStuff()</code> to fire twice.</p>
  
  <p>My workaround is to introduce a boolean where I effectively can negate this behavior, but sometimes it can get messy:</p>
  
  <pre><code>bool _preventStateChange;
  
  private void chkState_CheckedChanged(object sender, EventArgs e)
  {
      if (_preventStateChange)
          return;
      RedrawStuff();
  }
  
  private void numStateValue_ValueChanged(object sender, EventArgs e)
  {
      _preventStateChange = true;
      if (!chkState.Checked)
          chkState.Checked = true;
      RedrawStuff();
      _preventStateChange = false;
  }
  </code></pre>
  
  <p>Is this the best way to handle this situation?</p>