title: WinForms TreeView checking/unchecking hierarchy
body: <p>The following code is intended to recursively check or un-check parent or child nodes as required.</p>
  
  <p><a href="https://i.stack.imgur.com/EZ1wZ.png" rel="noreferrer"><img src="https://i.stack.imgur.com/EZ1wZ.png" alt="enter image description here"></a></p>
  
  <p>For instance, at this position, <strong>A</strong>, <strong>G</strong>, <strong>L</strong>, and <strong>T</strong> nodes must be unchecked if we un-check any one of them.</p>
  
  <p><a href="https://i.stack.imgur.com/JOJIB.png" rel="noreferrer"><img src="https://i.stack.imgur.com/JOJIB.png" alt="enter image description here"></a></p>
  
  <p>The problem with the following code is, whenever I double-click any node the algorithm fails to achieve its purpose.</p>
  
  <p>The tree-searching algorithm starts here:</p>
  
  <pre><code>    // stack is used to traverse the tree iteratively.
      Stack&lt;TreeNode&gt; stack = new Stack&lt;TreeNode&gt;();
      private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
      {
          TreeNode selectedNode = e.Node;
          bool checkedStatus = e.Node.Checked;
  
          // suppress repeated even firing
          treeView1.AfterCheck -= treeView1_AfterCheck;
  
          // traverse children
          stack.Push(selectedNode);
  
          while(stack.Count &gt; 0)
          {
              TreeNode node = stack.Pop();
  
              node.Checked = checkedStatus;                
  
              System.Console.Write(node.Text + ", ");
  
              if (node.Nodes.Count &gt; 0)
              {
                  ICollection tnc = node.Nodes;
  
                  foreach (TreeNode n in tnc)
                  {
                      stack.Push(n);
                  }
              }
          }
  
          //traverse parent
          while(selectedNode.Parent!=null)
          {
              TreeNode node = selectedNode.Parent;
  
              node.Checked = checkedStatus;
  
              selectedNode = selectedNode.Parent;
          }
  
          // "suppress repeated even firing" ends here
          treeView1.AfterCheck += treeView1_AfterCheck;
  
          string str = string.Empty;
      }
  </code></pre>
  
  <p><strong>Driver Program</strong></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Drawing;
  using System.Windows.Forms;
  
  namespace WindowsFormsApplication1
  {
      public partial class Form1 : Form
      {
          public Form1()
          {
              InitializeComponent();
          }
  
          #region MyRegion
          private void button1_Click(object sender, EventArgs e)
          {
              TreeNode a = new TreeNode("A");
              TreeNode b = new TreeNode("B");
              TreeNode c = new TreeNode("C");
              TreeNode d = new TreeNode("D");
              TreeNode g = new TreeNode("G");
              TreeNode h = new TreeNode("H");
              TreeNode i = new TreeNode("I");
              TreeNode j = new TreeNode("J");
              TreeNode k = new TreeNode("K");
              TreeNode l = new TreeNode("L");
              TreeNode m = new TreeNode("M");
              TreeNode n = new TreeNode("N");
              TreeNode o = new TreeNode("O");
              TreeNode p = new TreeNode("P");
              TreeNode q = new TreeNode("Q");
              TreeNode r = new TreeNode("R");
              TreeNode s = new TreeNode("S");
              TreeNode t = new TreeNode("T");
              TreeNode u = new TreeNode("U");
              TreeNode v = new TreeNode("V");
              TreeNode w = new TreeNode("W");
              TreeNode x = new TreeNode("X");
              TreeNode y = new TreeNode("Y");
              TreeNode z = new TreeNode("Z");
  
              k.Nodes.Add(x);
              k.Nodes.Add(y);
  
              l.Nodes.Add(s);
              l.Nodes.Add(t);
              l.Nodes.Add(u);
  
              n.Nodes.Add(o);
              n.Nodes.Add(p);
              n.Nodes.Add(q);
              n.Nodes.Add(r);
  
              g.Nodes.Add(k);
              g.Nodes.Add(l);
  
              i.Nodes.Add(m);
              i.Nodes.Add(n);
  
  
              j.Nodes.Add(b);
              j.Nodes.Add(c);
              j.Nodes.Add(d);
  
              a.Nodes.Add(g);
              a.Nodes.Add(h);
              a.Nodes.Add(i);
              a.Nodes.Add(j);
  
              treeView1.Nodes.Add(a);
              treeView1.ExpandAll();
  
              button1.Enabled = false;
          } 
          #endregion
  </code></pre>
  
  <p><strong>Expected to happen:</strong></p>
  
  <p>Take a look at the screenshot of the application. <strong><em>A</em></strong>, <strong><em>G</em></strong>, <strong><em>L</em></strong>, and <strong><em>T</em></strong> are checked. If I uncheck, say, <strong><em>L</em></strong>,<br>
   - <strong><em>T</em></strong> should be unchecked as <strong><em>T</em></strong> is a child of <strong><em>L</em></strong>.<br>
   - <strong><em>G</em></strong> and <strong><em>A</em></strong> should be unchecked as they will have no children left.</p>
  
  <p><strong>What is happening:</strong></p>
  
  <p>This application code works fine if I single-click any node. If I double-click a node, that node becomes checked/unchecked but the same change is not reflected on the parent and children. </p>
  
  <p>Double-click also freezes the application for a while.</p>
  
  <p><strong>How can I fix this issue and obtain the expected behavior?</strong></p>