如何在treeview中找到根节点 [英] How can i find root node in treeview

查看:241
本文介绍了如何在treeview中找到根节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我的Windows应用程序中有一个树视图。


这个树视图中的
用户可以添加一些根节点这些根节点的一些子节点以及这些子节点的一些子节点等等。



我的意思是这样的:



Hi everybody,
I have a treeview in my windows application.

in this treeview users 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.

I mean something like this :

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



现在我的问题是,如果我有'E'节点,找到第一个根节点的最佳方法是什么('root1')?


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

推荐答案

每个 TreeNode 都有一个 Parent 属性只是向上递归,直到 Parent null
Each TreeNode has a Parent property just recurse upwards until the Parent is null.


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


这篇关于如何在treeview中找到根节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆