title: Recursively collapse all child nodes of parent node in custom UserControl
body: <p>I actually have a custom-built UserControl similar to a TreeView. The only thing it's missing is the <code>CollapseAll()</code> method from a TreeView, so I'm looking to either a) find the source code for the CollapseAll method so I can copy it, or b) manually (and recursively) iterate through all the child nodes of a selected node and collapse them.</p>
  
  <p>Here is my current collapse method:</p>
  
  <pre><code>private void OnNodeCollapsed(TreeNode node) {
      foreach(FileSystemNode n in node.Nodes) {
          if(n.NodeType != FileSystemNodeType.Computer &amp;&amp; n.NodeType != FileSystemNodeType.Drive) {
              if(n.Nodes.Count &gt; 0) {
                  n.Nodes.Clear();
              }
          }
      }
  
      foreach(TreeNode child in node.Nodes) {
          // and this is where I lose it
      }
  }
  </code></pre>
  
  <p>I only want to collapse <em>child nodes</em>, not the whole tree. Is there a way of doing that?</p>
  
  <p><strong>A good solution is this: <a href="https://stackoverflow.com/questions/9126675/recursion-with-yield-return-elements-order-in-tree">Recursion with yield return elements order in tree</a>.</strong></p>