how to find child nodes at root node [TreeView]

前端 未结 3 1308
野趣味
野趣味 2021-01-01 01:05
 ROOT
      A 
        B
          C 
            D 
              E
        T
      F
      G
        X

I want to find E Node\'s parent nodes(it i

3条回答
  •  情书的邮戳
    2021-01-01 01:42

    I would suggest using recursive iterations.

    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.

    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 is the value you are after.

提交回复
热议问题