title: C# - Find a specific node or subnode of a treeview control
body: <p>I have a problem with the TreeView control in WinForm. The thing is, I need to automate the process of inserting nodes by using a while loop and using the node previously inserted in the control as the parent node search criterion.</p>
  <p>Here my problem arises, when I look for the node in the control I use this code:</p>
  <pre><code>foreach (TreeNode singleNode in Repository_TreeView.Nodes)
  {
      if (singleNode.Text.Contains(specificPTN) == true)
      {
          Repository_TreeView.SelectedNode = singleNode;
      }
  </code></pre>
  <p>In this way, however, I only get the nodes in the highest level of the hierarchy. So, for example:</p>
  <pre><code>/--------------------------------------------------\
  |    + rootnode1                                   |
  |    |        +---&gt; childnode1                     |
  |    |                      +---&gt; grandchildnode1  |
  |    |                      |                      |
  |    |                      +---&gt; grandchildnode1  |
  |    |                                             |
  |    + rootnode2                                   |
  |             +---&gt; childnode2                     |
  |                           +---&gt; grandchildnode2  |
  |                           |                      |
  |                           +---&gt; grandchildnode2  |
  |                                                  |
  |                                                  |
  \--------------------------------------------------/
  </code></pre>
  <p>In this case my code would only get the node 'rootnode1' and 'rootnode2', when I need to get all the other subnodes instead.</p>
  <p>I also tried using this lambda expression to look up the node by name:</p>
  <pre><code>TreeNode[] parentNodes = Repository_TreeView.Nodes
                                      .Cast&lt;TreeNode&gt;()
                                      .Where(r =&gt; r.Text == specificPTN)
                                      .ToArray();
  </code></pre>
  <p>The result, however, is always the same, 'rootnode1' and 'rootnode2' are always found.</p>
  <p>I hope someone can help me find a way to get all the nodes and subnodes out of control. Thanks and sorry for the long explanation.</p>