Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find a root node in TreeView

I have a TreeView in my Windows application. Tn this TreeView, the user can add some root nodes and also some sub nodes for these root nodes and also some sub nodes for these sub nodes and so on ...

For example:

Root1
     A
       B
         C
         D
          E  
Root2
     F
      G
.
.
.

Now my question is that if I am at node 'E' what is the best way to find its first root node ('Root1')?

like image 394
M_Mogharrabi Avatar asked Nov 26 '11 07:11

M_Mogharrabi


1 Answers

Here is a little method for you:

private TreeNode FindRootNode(TreeNode treeNode)
{
    while (treeNode.Parent != null)
    {
        treeNode = treeNode.Parent;
    }
    return treeNode;
}

you can call in your code like this:

var rootNode = FindRootNode(currentTreeNode);
like image 108
Fischermaen Avatar answered Nov 18 '22 01:11

Fischermaen