gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 17:07:10 25 4
gpt4 key购买 nike

我正在选择 form

中的所有控件

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

我需要这样的东西:(这是我的代码)

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

有什么想法吗?

谢谢

最佳答案

你需要使用递归。像这样的方法应该足够了

IEnumerable<TreeNode> Collect(TreeNodeCollection nodes)
{
foreach(TreeNode node in nodes)
{
yield return node;

foreach (var child in Collect(node.Nodes))
yield return child;
}
}

然后在你的方法中你可以做

 foreach (var node in Collect(tree.Nodes))
{
// you will see every child node here
}

关于c# - 如何遍历 treeView 控件的所有节点。 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19691286/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com