Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>myTreeview.Nodes will give you a list of root nodes as defined by MS which basically means nodes on the root branch of the tree.</p> <p>This code will build a list of root nodes with children:</p> <pre><code> IList&lt;TreeNode&gt; nodesWithChildren = new List&lt;TreeNode&gt;(); foreach( TreeNode node in myTreeview.Nodes ) if( node.Nodes.Count &gt; 0 ) nodesWithChildren.Add( node ); </code></pre> <hr> <p>Update: and if you wanted all nodes in the TreeView that had a child regardless of how deep into the tree then use a bit of recursion, e.g.</p> <pre><code>private static IList&lt;TreeNode&gt; BuildParentNodeList(TreeView treeView) { IList&lt;TreeNode&gt; nodesWithChildren = new List&lt;TreeNode&gt;(); foreach( TreeNode node in treeView.Nodes ) AddParentNodes(nodesWithChildren, node); return nodesWithChildren; } private static void AddParentNodes(IList&lt;TreeNode&gt; nodesWithChildren, TreeNode parentNode) { if (parentNode.Nodes.Count &gt; 0) { nodesWithChildren.Add( parentNode ); foreach( TreeNode node in parentNode.Nodes ) AddParentNodes( nodesWithChildren, node ); } } </code></pre> <hr> <p>Update 2: Recursion method with only 1 foreach loop:</p> <pre><code>private static IList&lt;TreeNode&gt; BuildParentNodeList(TreeView treeView) { IList&lt;TreeNode&gt; nodesWithChildren = new List&lt;TreeNode&gt;(); AddParentNodes( nodesWithChildren, treeView.Nodes ); return nodesWithChildren; } private static void AddParentNodes(IList&lt;TreeNode&gt; nodesWithChildren, TreeNodeCollection parentNodes ) { foreach (TreeNode node in parentNodes) { if (node.Nodes.Count &gt; 0) { nodesWithChildren.Add( node ); AddParentNodes(nodesWithChildren, node.Nodes); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload