title: Checkbox to (un)check all other checkboxes
body: <p>I've created a windows form which (so far) contains only checkboxes. The constructor takes one parameter: <code>string[] attributes</code>. For each string in this <code>attributes</code> array I create a checkbox.</p>
  
  <p>For example:</p>
  
  <pre><code>string[] attributes = {
                            "Black",
                            "Red",
                            "Blue"
                        };
  form1 = new MyForm(attributes);
  form1.Show();
  </code></pre>
  
  <p>Will create checkboxes like so:</p>
  
  <pre><code>[ ] Black
  [ ] Red
  [ ] Blue
  </code></pre>
  
  <p>This works just fine. Now my next step is to create a checkbox "Check All", which has the following behavior. I'll use <code>this</code> to refer to my <em>"Check All" checkbox</em>.</p>
  
  <p>When:</p>
  
  <ul>
  <li>User checks <code>this</code>: All other checkboxes get checked.</li>
  <li>User unchecks <code>this</code>: All other checkboxes get unchecked.</li>
  <li>All other checkboxes get manually checked: <code>this</code> gets checked too.</li>
  <li>All checkboxes are checked and any of them gets unchecked: <code>this</code> gets unchecked too.</li>
  </ul>
  
  <p>I managed to do all of the above rules but I ran into a problem and I can't figure out how to fix it: When all checkboxes are checked and user unchecks one checkbox, it means that my <em>"Check All" checkbox</em> will get unchecked too. Now that my <em>"Check All" checkbox</em> got unchecked, it automatically calls for uncheck event which then unchecks ALL of the checkboxes as if the user had unchecked my <em>"Check All" checkbox</em>.</p>
  
  <p>So is there a way to tell my checkbox not to run <code>CheckedChanged</code> when it's an other checkbox calling the uncheck?</p>
  
  <p>Here's my code (it's all written by hand, so no visual studio designer used):</p>
  
  <pre><code>using System;
  using System.Drawing;
  using System.Windows.Forms;
  
  class MyForm
  {
      public MyForm(string[] attributes)
      {
          SpawnControls(attributes);
      }
  
      private CheckBox[] m_attributes;
      private CheckBox m_all;
  
      private void SpawnControls(string[] attributes)
      {
          CheckBox dummy = new CheckBox();
          int nAttr = attributes.Length;
  
          m_attributes = new CheckBox[nAttr];
          for (int i = 0; i &lt; nAttr; i++)
          {
              m_attributes[i] = new CheckBox();
              m_attributes[i].Text = attributes[i];
              m_attributes[i].Location = new Point(5, dummy.Height * i);
              m_attributes[i].CheckedChanged += attribute_CheckedChanged;
              Controls.Add(m_attributes[i]);
          }
  
          m_all = new CheckBox();
          m_all.Text = "Check All";
          m_all.Location = new Point(5, m_attributes[nAttr - 1].Bottom);
          m_all.CheckedChanged += all_CheckedChanged;
          Controls.Add(m_all);
      }
  
      private void attribute_CheckedChanged(object sender, EventArgs e)
      {
          if (((CheckBox)sender).Checked)
          {
              foreach (CheckBox cb in m_attributes)
              {
                  if (cb.Checked == false)
                  {
                      return;
                  }
              }
              m_all.Checked = true;
          }
          else if (m_all.Checked)
          {
              m_all.Checked = false;
          }
      }
  
      private void all_CheckedChanged(object sender, EventArgs e)
      {
          if (m_all.Checked)
          {
              foreach (CheckBox cb in m_attributes)
              {
                  cb.Checked = true;
              }
          }
          else
          {
              foreach (CheckBox cb in m_attributes)
              {
                  cb.Checked = false;
              }
          }
      }
  }
  </code></pre>