Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is a snippet of code that I use to perform this task from my core library. It allows you to list the nodes either depth first or breath first without the use of recursion, which has the overhead of constructing stackframes in the JIT engine. Its very fast.</p> <p>To use it simply go:</p> <p>List&lt; TreeNode > nodes = TreeViewUtils.FlattenDepth(tree);</p> <p>Sorry you you VB heads, I can't give an example, but I'm sure you'll work it out.</p> <pre><code>public class TreeViewUtils { /// &lt;summary&gt; /// This static utiltiy method flattens all the nodes in a tree view using /// a queue based breath first search rather than the overhead /// of recursive method calls. /// &lt;/summary&gt; /// &lt;param name="tree"&gt;&lt;/param&gt; /// &lt;returns&gt;&lt;/returns&gt; public static List&lt;TreeNode&gt; FlattenBreath(TreeView tree) { List&lt;TreeNode&gt; nodes = new List&lt;TreeNode&gt;(); Queue&lt;TreeNode&gt; queue = new Queue&lt;TreeNode&gt;(); // // Bang all the top nodes into the queue. // foreach(TreeNode top in tree.Nodes) { queue.Enqueue(top); } while(queue.Count &gt; 0) { TreeNode node = queue.Dequeue(); if(node != null) { // // Add the node to the list of nodes. // nodes.Add(node); if(node.Nodes != null &amp;&amp; node.Nodes.Count &gt; 0) { // // Enqueue the child nodes. // foreach(TreeNode child in node.Nodes) { queue.Enqueue(child); } } } } return nodes; } /// &lt;summary&gt; /// This static utiltiy method flattens all the nodes in a tree view using /// a stack based depth first search rather than the overhead /// of recursive method calls. /// &lt;/summary&gt; /// &lt;param name="tree"&gt;&lt;/param&gt; /// &lt;returns&gt;&lt;/returns&gt; public static List&lt;TreeNode&gt; FlattenDepth(TreeView tree) { List&lt;TreeNode&gt; nodes = new List&lt;TreeNode&gt;(); Stack&lt;TreeNode&gt; stack = new Stack&lt;TreeNode&gt;(); // // Bang all the top nodes into the queue. // foreach(TreeNode top in tree.Nodes) { stack.Push(top); } while(stack.Count &gt; 0) { TreeNode node = stack.Pop(); if(node != null) { // // Add the node to the list of nodes. // nodes.Add(node); if(node.Nodes != null &amp;&amp; node.Nodes.Count &gt; 0) { // // Enqueue the child nodes. // foreach(TreeNode child in node.Nodes) { stack.Push(child); } } } } return nodes; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    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. 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