C#中 TreeView如何获取某一节点下的所有子节点

2024-12-22 18:39:20
推荐回答(1个)
回答1:

这个好办,通过递归调用,把节点放到一个list里不就得了

namespace WindowsApplication1
{
public partial class Form1 : Form
{
private List nodeList = new List();

private void FetchNode(TreeNode node)
{
nodeList.Add(node);
for (int i = 0; i < node.Nodes.Count; i++)
{
FetchNode(node.Nodes[i]);
}
}

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < this.treeView1.Nodes.Count; i++)
{
FetchNode(this.treeView1.Nodes[i]);//递归根节点的所有子节点
}
MessageBox.Show(nodeList.Count + "");
}
}
}