Need root-node of clicked node! How??

Hi there,

supposing the following tree:

root_A
   A_1
      A_1_1
      A_1_2
   A_2
root_B
   B_1
   B_2
   B_3
      B_3_1

How can I get the corresponding root, when A_1_2 is selected

this.treeView1.SelectedNode.Index;

just brings out the index relative to the branch it is in...


Thanks,
Finch.
  


Answer this question

Need root-node of clicked node! How??

  • cgerull

    So check if its a base node:
    if(node.Parent == null) // has no parent, so it's a base node

    to check if the node is the first in the tree, do:
    if(treeView.Nodes[0] == node)



  • Kroeze

    What would be the base case for the recursive-function
    I can't think of one! :-/

    public string getRootNode(TreeNode node)
    {
       if ("node is not root")
       {
          node = node.Parent();
          this.getRootNode(node);
       }//if
       else
       {
          return node.ToString();
       }//else

    }//getRootNode

    Should be something like that, but what is the base-case Trying to get the Parent-node of a root-node would probably cause an exception...

    Thanks,
    Finch.

  • Mike Ut

    A_1 = A_1_2.ParentNode
    root_A = A_1_2.ParentNode.ParentNode

    and so on. Just make it recursive


  • Need root-node of clicked node! How??