How to find node root in TreeView

I have a TreeView in my windows application. Tn is a TreeView, the user can add some root nodes, as well as some sub-carbon for these root nodes, as well as some sub-carbon for these sub nodes, and so on ...

For example:

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

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

+7
source share
1 answer

Here is a small method for you:

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

You can call your code as follows:

 var rootNode = FindRootNode(currentTreeNode); 
+13
source

All Articles