如何确定如果选择的节点是树视图中的一个子或父节点? [英] How can I determine if the selected node is a child or parent node in TreeView?
问题描述
我如何才能知道如果选择的节点是一个子节点或在的TreeView
控制父节点?
How can I find out if the selected node is a child node or a parent node in the TreeView
control?
推荐答案
你到底如何实现这样的检查取决于你如何定义儿童与父节点。但有两个属性由每个树节点
对象公开能够提供重要的信息:
Exactly how you implement such a check depends on how you define "child" and "parent" nodes. But there are two properties exposed by each TreeNode
object that provide important information:
-
的<一个href="http://msdn.microsoft.com/en-us/library/system.windows.forms.treenode.nodes.aspx"><$c$c>Nodes$c$c>物业返回
树节点
的集合对象包含的特定节点。因此,通过简单的检查,看看有多少子节点选择的节点包含,就可以判断它是否是一个父节点:
The
Nodes
property returns the collection ofTreeNode
objects contained by that particular node. So, by simply checking to see how many child nodes the selected node contains, you can determine whether or not it is a parent node:
if (selectedNode.Nodes.Count == 0)
{
MessageBox.Show("The node does not have any children.");
}
else
{
MessageBox.Show("The node has children, so it must be a parent.");
}
要获取更多信息,您还可以检查父
属性的值。如果该值空
,则节点位于的TreeView
(它没有父的根级别):
To obtain more information, you can also examine the value of the Parent
property. If this value is null
, then the node is at the root level of the TreeView
(it does not have a parent):
if (selectedNode.Parent == null)
{
MessageBox.Show("The node does not have a parent.");
}
else
{
MessageBox.Show("The node has a parent, so it must be a child.");
}
这篇关于如何确定如果选择的节点是树视图中的一个子或父节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!