AdvTree控件时一款实用且功能强大的树型控件,包含在DotNetBar for Windows Forms里,在使用树型控件时,经常需要对树型的所有
节点进行遍历,以便得到节点信息,进行数据节点的相关处理,这里主要介绍如何快速的遍历AdvTree所有树型节点。
// Following code enumerates through all nodes and prints out node text to console
foreach (Node node in AllNodes(advTree1.Nodes))
{
Console.WriteLine(node.Text);
}
// This is what makes flat access to tree nodes easy
private static IEnumerable AllNodes(NodeCollection nodes)
{
for (int i = 0; i<nodes.Count; i++)
{
Node node = nodes[i];
yield return node;
if (node.Nodes.Count > 0)
{
foreach (Node item in AllNodes(node.Nodes))
{
yield return item;
}
}
}
}