如何在根节点找到子节点 [TreeView] [英] how to find child nodes at root node [TreeView]

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

问题描述

 ROOT
      A 
        B
          C 
            D 
              E
        T
      F
      G
        X

我想找到 E 节点的父节点(编号为 5).然后,我将保存节点.如果数字小于 5.我在 Asp.net 控件中使用 TreeView.

I want to find E Node's parent nodes(it is number 5). Then, I'll save node. If number is smaller 5. I'm using TreeView in Asp.net control.

推荐答案

我建议使用递归迭代.

private TreeNode FindNode(TreeView tvSelection, string matchText) 
{ 
    foreach (TreeNode node in tvSelection.Nodes) 
    { 
        if (node.Tag.ToString() == matchText) 
        {
            return node; 
        }
        else 
        { 
            TreeNode nodeChild = FindChildNode (node, matchText); 
            if (nodeChild != null) return nodeChild; 
        } 
    } 
    return (TreeNode)null; 
}

您可以利用此逻辑来确定关于您的节点的许多事情,并且此结构还允许您扩展您可以对节点执行的操作以及您希望搜索的条件.您可以编辑我的示例以满足您自己的需要.

You can utilize this logic to determine many things about you node and this structure also allows you to expand what you can do with the node and the criteria you wish to search for. You can edit my example to fit your own needs.

因此,在这个例子中,你可以传入 E 并期望返回节点 E 然后简单地如果返回的节点的父属性将是您所追求的父.

Thus, with this example you could pass in E and expect to have the node E returned then simply if the parent property of the node returned would be the parent you are after.

tn treenode = FindNode(myTreeview, "E")

tn.parent 是您所追求的值.

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

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