How to get a list of all child nodes in a TreeView in .NET

后端 未结 10 1091
抹茶落季
抹茶落季 2020-12-15 21:28

I have a TreeView control in my WinForms .NET application that has multiple levels of childnodes that have childnodes with more childnodes, with no defined depth. When a use

10条回答
  •  眼角桃花
    2020-12-15 21:50

    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.

    To use it simply go:

    List< TreeNode > nodes = TreeViewUtils.FlattenDepth(tree);
    

    Sorry, you have a VB.Net tag; I can't give an example, but I'm sure you'll work it out.

    public class TreeViewUtils
    {
        /// 
        /// 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.
        /// 
        /// 
        /// 
        public static List FlattenBreath(TreeView tree) {
            List nodes = new List();
    
            Queue queue = new Queue();
    
            //
            // Bang all the top nodes into the queue.
            //
            foreach(TreeNode top in tree.Nodes) {
                queue.Enqueue(top);
            }
    
            while(queue.Count > 0) {
                TreeNode node = queue.Dequeue();
                if(node != null) {
                    //
                    // Add the node to the list of nodes.
                    //
                    nodes.Add(node);
    
                    if(node.Nodes != null && node.Nodes.Count > 0) {
                        //
                        // Enqueue the child nodes.
                        //
                        foreach(TreeNode child in node.Nodes) {
                            queue.Enqueue(child);
                        }
                    }
                }
            }
            return nodes;
        }
    
        /// 
        /// 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.
        /// 
        /// 
        /// 
        public static List FlattenDepth(TreeView tree) {
            List nodes = new List();
    
            Stack stack = new Stack();
    
            //
            // Bang all the top nodes into the queue.
            //
            foreach(TreeNode top in tree.Nodes) {
                stack.Push(top);
            }
    
            while(stack.Count > 0) {
                TreeNode node = stack.Pop();
                if(node != null) {
    
                    //
                    // Add the node to the list of nodes.
                    //
                    nodes.Add(node);
    
                    if(node.Nodes != null && node.Nodes.Count > 0) {
                        //
                        // Enqueue the child nodes.
                        //
                        foreach(TreeNode child in node.Nodes) {
                            stack.Push(child);
                        }
                    }
                }
            }
            return nodes;
        }
    }
    

提交回复
热议问题