C# 如何遍历 treeView 控件的所有节点。C#

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19691286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 15:37:57  来源:igfitidea点击:

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

c#loopstreeviewnodes

提问by Jean

I am selecting all controls I have in a form

我正在选择我拥有的所有控件 form

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

如果控件是Treeviews,我将迭代它们拥有的所有节点

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

谢谢你

采纳答案by Darren Kopp

Your mistake is that cis actually a variable of type Control, which does not have a Nodesmember. You will need it to cast it as a TreeViewtype.

你的错误是它c实际上是一个 type 变量Control,它没有Nodes成员。您将需要它来将其转换为TreeView类型。

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;
        ...
}

回答by Darren Kopp

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
 }

回答by Jim Mischel

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);

回答by user3217996

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 == "")
                {

                }
            }
        }
    }

回答by Plasmabubble

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

在 Darren 的出色答案的基础上,您可以结合使用recursion 和 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.

注意方法的第一个参数之前的“this”。

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

然后你可以在所有树视图中使用这个新方法:

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

回答by Bob Oberst

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).

与此处发布的一些示例不同,我实际上对此进行了测试(哎哟!我可以听到他们说)。