How to Iterate through all nodes of a treeView Control. C#

倖福魔咒の 提交于 2019-12-17 19:31:07

问题


I am selecting all controls I have in a form

if controls are Treeviews, I'll iterate all nodes they have

I need something like: (And it is my code)

foreach (Control c in PanelSM.Controls)
{
    if (c is TreeView) 
    {    
        TreeNodeCollection myNodes = c.Nodes;//<<<<< Here is a mistake
        foreach (TreeNode n in myNodes)
        {
            String text = rm.GetString(n.Name);
            //And more things
            //...
            //...
            //...
       }
    }
    //...
}

Any idea?

Thank You


回答1:


Your mistake is that c is actually a variable of type Control, which does not have a Nodes member. You will need it to cast it as a TreeView type.

You can do either of these two approaches:

if (c is TreeView) 
{
    TreeNodeCollection myNodes = ((TreeView) c).Nodes; // <<--- Note the cast
    ...
}

or

TreeView tv = c as TreeView;
if (tv != null)
{
        TreeNodeCollection myNodes = tv.Nodes;
        ...
}



回答2:


You need to use recursion. A method like this should suffice

IEnumerable<TreeNode> Collect(TreeNodeCollection nodes)
{
    foreach(TreeNode node in nodes)
    {
        yield return node;

        foreach (var child in Collect(node.Nodes))
            yield return child;
    }
}

Then in your method you can just do

 foreach (var node in Collect(tree.Nodes))
 {
     // you will see every child node here
 }



回答3:


It's pretty easy:

void TraverseTree(TreeNodeCollection nodes)
{
    foreach (var child in nodes)
    {
        DoSomethingWithNode(child);
        TraverseTree(child.Nodes);
    }
}

And call it with:

TraverseTree(MyTreeView.Nodes);



回答4:


I prefer simplicity, and here is my simple solution:

    protected void TraverseNodes(TreeNodeCollection nodes, string action, int maxDepth = 2) 
    {
        foreach (TreeNode node in nodes)
        {
            if (node.ChildNodes.Count > 0 && node.Depth < maxDepth)
                TraverseNodes(node.ChildNodes, action, maxDepth);

            //do something!!!
            var x = node.Text;
            node.Checked = !node.Checked;
        }
    }

I decided to include a "maximum depth" as a bonus, so enjoy.

Call it as follows:

                TraverseNodes(this.Activities.Nodes, "");

Unlike some of the examples posted here, I actually tested this (ouch! I can hear them say).




回答5:


try this

    foreach (TreeNode t in tvMenu.Nodes)
    {
        for (int iParent = 0; iParent < t.ChildNodes.Count; iParent++)
        {
            for (int iChild = 0; iChild < t.ChildNodes[iParent].ChildNodes.Count; iChild++)
            {
                if (t.ChildNodes[iParent].ChildNodes[iChild].Text == "")
                {

                }
            }
        }
    }



回答6:


Building on top of Darren's great answer, you can combine recursion and class extension.

Declare somewhere in your namespace :

public static class MyExtensions
{
    public static IEnumerable<TreeNode> All( this TreeNodeCollection nodes )
    {
        foreach( TreeNode n in nodes )
        {
            yield return n;
            foreach( TreeNode child in n.Nodes.All( ) )
                yield return child;
        }
    }
}

Note the "this" before the first argument of the method.

Then you can use this new method in all treeviews as:

foreach( TreeNode n in myTreeview.Nodes.All() ) ...


来源:https://stackoverflow.com/questions/19691286/how-to-iterate-through-all-nodes-of-a-treeview-control-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!