在Visual C#中的树视图控件中列出父节点的所有子节点 [英] List all child nodes of a parent node in a treeview control in Visual C#

查看:55
本文介绍了在Visual C#中的树视图控件中列出父节点的所有子节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个treeview控件,它包含一个父节点和该父节点的几个子节点.有没有一种方法可以从主父节点获取所有子节点的数组或列表?即从treeview.nodes [0]或第一个父节点获取所有节点.

I have a treeview control, and it contains a single parent node and several child nodes from that parent. Is there a way to obtain an array or list of all the child nodes from the main parent? i.e. getting all the nodes from treeview.nodes[0], or the first parent node.

推荐答案

您可以像这样递归地添加到列表中:

You can add to a list recursively like this:

public void AddChildren(List<TreeNode> Nodes, TreeNode Node)
{
    foreach (TreeNode thisNode in Node.Nodes)
    {
        Nodes.Add(thisNode);
        AddChildren(Nodes, thisNode);
    }
}

然后调用此例程,将其传递到根节点:

Then call this routine passing in the root node:

List<TreeNode> Nodes = new List<TreeNode>();
AddChildren(Nodes, treeView1.Nodes[0]);

这篇关于在Visual C#中的树视图控件中列出父节点的所有子节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆