如何确定所选节点是否为TreeView中的子节点或父节点?

时间:2022-05-25 13:38:50

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

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

5 个解决方案

#1


30  

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:

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

  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:

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

    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):

    要获取更多信息,还可以检查Parent属性的值。如果此值为null,则该节点位于TreeView的根级别(它没有父级):

    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.");
    }
    

#2


5  

You can use the TreeNode.Parent property for this.

您可以使用TreeNode.Parent属性。

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

#3


1  

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.";
   }
}

#4


0  

treeview.SelectedNode == null

is the best to choose.

是最好的选择。

#5


0  

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

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

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

#1


30  

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:

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

  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:

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

    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):

    要获取更多信息,还可以检查Parent属性的值。如果此值为null,则该节点位于TreeView的根级别(它没有父级):

    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.");
    }
    

#2


5  

You can use the TreeNode.Parent property for this.

您可以使用TreeNode.Parent属性。

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

#3


1  

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.";
   }
}

#4


0  

treeview.SelectedNode == null

is the best to choose.

是最好的选择。

#5


0  

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

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

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