如何确定所选节点是 TreeView 中的子节点还是父节点?

发布于 2024-11-01 17:29:47 字数 52 浏览 1 评论 0原文

如何确定所选节点是 TreeView 控件中的子节点还是父节点?

How can I find out if the selected node is a child node or a parent node in the TreeView control?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

£冰雨忧蓝° 2024-11-08 17:29:47

具体如何实现此类检查取决于您如何定义“子”和“父”节点。但每个 TreeNode 对象公开了两个属性,它们提供了重要信息:

  1. Nodes 属性 返回该特定节点包含的 TreeNode 对象的集合。因此,只需检查所选节点包含多少个子节点,就可以确定它是否是父节点:

    if (selectedNode.Nodes.Count == 0)
    {
        MessageBox.Show("该节点没有任何子节点。");
    }
    别的
    {
        MessageBox.Show("该节点有子节点,因此它必须是父节点。");
    }
    
  2. 要获取更多信息,您还可以检查 Parent属性。如果此值为 null,则该节点位于 TreeView 的根级别(它没有父级):

    if (selectedNode.Parent == null)
    {
        MessageBox.Show("该节点没有父节点。");
    }
    别的
    {
        MessageBox.Show("该节点有父节点,因此它必须是子节点。");
    }
    

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. 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.");
    }
    
  2. 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.");
    }
    
野鹿林 2024-11-08 17:29:47

您可以使用 TreeNode .Parent 属性。

如果其值为 null 引用,则该节点位于根级别。

TreeView treeView = ...
var selectedNode = treeView.SelectedNode;

if(selectedNode ! = null)
{
    if(selectedNode.Parent == null)  
    {     
        // Root-level node  
    }
    else 
    {     
        // Child node
    } 
}
else
{
    // A node hasn't been selected.
}

You can use the TreeNode.Parent property for this.

If its value is a null-reference, the node is at the root level.

TreeView treeView = ...
var selectedNode = treeView.SelectedNode;

if(selectedNode ! = null)
{
    if(selectedNode.Parent == null)  
    {     
        // Root-level node  
    }
    else 
    {     
        // Child node
    } 
}
else
{
    // A node hasn't been selected.
}
许仙没带伞 2024-11-08 17:29:47

试试这个

private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{  
   Label1.Text = "";
   if(e.Node.Parent!= null && 
     e.Node.Parent.GetType() == typeof(TreeNode) )
   {
      Label1.Text = "Parent: " + e.Node.Parent.Text + "\n"
         + "Index Position: " + e.Node.Parent.Index.ToString();
   }
   else
   {
      Label1.Text = "This is parent node.";
   }
}

Try this

private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{  
   Label1.Text = "";
   if(e.Node.Parent!= null && 
     e.Node.Parent.GetType() == typeof(TreeNode) )
   {
      Label1.Text = "Parent: " + e.Node.Parent.Text + "\n"
         + "Index Position: " + e.Node.Parent.Index.ToString();
   }
   else
   {
      Label1.Text = "This is parent node.";
   }
}
[浮城] 2024-11-08 17:29:47

对于根节点是父 TreeView .. 可以检查我们是否比较 -> 的类型

if (currentNode.Parent.GetType() == typeof(TreeView)) 
{
    // root node
}

For root node is the parent TreeView .. it is possible to check if we compare the types of ->

if (currentNode.Parent.GetType() == typeof(TreeView)) 
{
    // root node
}
赏烟花じ飞满天 2024-11-08 17:29:47
treeview.SelectedNode == null

是最好的选择。

treeview.SelectedNode == null

is the best to choose.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文