如何通过一个TreeView控件的所有节点进行迭代。 C# [英] How to Iterate through all nodes of a treeView Control. C#
本文介绍了如何通过一个TreeView控件的所有节点进行迭代。 C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我选择我有一个格式
如果控件是所有控件树视图
,我会遍历所有节点他们有
if controls are Treeviews
, I'll iterate all nodes they have
我需要的是这样的:
(它是我的代码)
I need something like: (And it is my code)
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
//...
//...
//...
}
}
//...
}
任何想法?
感谢您
推荐答案
您的错误是 C
实际上是类型的变量控制
,它不具有的节点的成员。你会需要它强制转换为一个 TreeView控件
键入
Your mistake is that c
is actually a variable of type Control
, which does not have a Nodes member. You will need it to cast it as a TreeView
type.
您可以执行这两种方法:
You can do either of these two approaches:
if (c is TreeView)
{
TreeNodeCollection myNodes = ((TreeView) c).Nodes; // <<--- Note the cast
...
}
或
TreeView tv = c as TreeView;
if (tv != null)
{
TreeNodeCollection myNodes = tv.Nodes;
...
}
这篇关于如何通过一个TreeView控件的所有节点进行迭代。 C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文