访问 TreeView 控件中的所有节点 [英] Accessing all the nodes in TreeView Control
问题描述
我有一个带有一组节点和子节点的 TreeView
控件.例如:
I have a TreeView
Control with set of nodes and child nodes. For example:
ROOT 有 A、B、C.
ROOT has A,B,C.
A 有 a1、a2、a3,然后 a1、a2 还包含一些节点,如 x1、x2、x3 等.像这样有很多子节点.我知道可以在 for 循环中使用循环.
A has a1, a2, a3 and then that a1, a2 also contains some nodes like x1, x2, x3 and so on. Like this many subnodes are there. I know it is possible to use loops with a for loop.
我只想使用一两个 for 循环访问 TreeView
控件中的所有节点.
I just want to access all the nodes in TreeView
control using one or two for loops.
是否有任何算法或其他方法?
Is there any algorithm for that or is there any other way?
再问一个问题:是否可以使用任何库函数在对象或字符串中获得树节点的路径?例如:
One more question: Is it is possible to have the path of a tree node in an object or in a string using any library functions? For example:
string S = TreeView1.Nodes[i].Nodes[j].Nodes
推荐答案
不要使用嵌套循环,而是使用递归解决方案,例如:
Don't use nested loops, but go for an recursive solution like:
void ListNodes( TreeNode node )
{
foreach( var subnode in node.Nodes )
{
ListNodes( subnode );
}
// Print out node
}
为您的根节点调用此函数.
Call this function for your root node.
对于您的其他问题:检查 FullPath
属性.
For your additional question: check the FullPath
property.
这篇关于访问 TreeView 控件中的所有节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!