如何确定如果选择的节点是树视图中的一个子或父节点? [英] How can I determine if the selected node is a child or parent node in TreeView?

查看:153
本文介绍了如何确定如果选择的节点是树视图中的一个子或父节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何才能知道如果选择的节点是一个子节点或在的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:

  1. 的<一个href="http://msdn.microsoft.com/en-us/library/system.windows.forms.treenode.nodes.aspx"><$c$c>Nodes物业返回树节点的集合对象包含的特定节点。因此,通过简单的检查,看看有多少子节点选择的节点包含,就可以判断它是否是一个父节点:

  1. The Nodes property returns the collection of TreeNode 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屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆