How to Find Child Nodes in a TreeView Control

This will hopefully be a simple question...

I have a treeview control (Treeview1) that gets filled programmatically with different numbers of nodes and child nodes. I am trying to find out how many child nodes one particular node has and what the text of these child nodes are. I know the parent node's name is "Drives" and that each of the child nodes under this node will be a drive letter followed by a colon and backslash (such as C:\ or D:\).

Is there a way to find how many of these child nodes exist and what their names are

I'm not sure if there's an easy way to use Nodes.Find, but it seems to be leading me nowhere.

What's a good way to accomplish this

Thanks.

-- Jim



Answer this question

How to Find Child Nodes in a TreeView Control

  • mr. robot

    Any chance you can you translate that into VB

    Thanks for your help.

    -- Jim


  • sindhu

    I don't see the problem, you can iterator over the TreeNodes.


  • poopiman

    Thanks, ReneeC... I didn't realize that each node also has a Nodes property. I thought you could only get to that from the TreeView object. That makes life a lot easier!

    -- Jim


  • shron

    I've writen a little example:


    public static TreeNode[] GetInnerNodes( TreeNode node )
    {
    System.Collections.ArrayList nodeBuffer = new System.Collections.ArrayList( node.Nodes.Count );

    foreach( TreeNode innerNode in node.Nodes )
    {
    TreeNode[] innerNodes = GetInnerNodes( innerNode );
    nodeBuffer.AddRange( innerNodes );
    nodeBuffer.Add( innerNode );
    }

    return (TreeNode)nodeBuffer.ToArray( typeof( TreeNode ) );
    }



    The method will give any nodes that are in a specified TreeNode in a Array. The code is writen out of the head, but i think you will get the point. You can use the Lenght property of the returned array and iterate over it to get specific properties like Text.


  • Richard Broida

    Well, if I try to iterate through them using something like...

    Dim Chk4Drives As TreeNode
    For Each Chk4Drives In Me.TreeView1.Nodes
    If Chk4Drives.Text = "C:\" Then
    MsgBox(Chk4Drives.Index)
    End If
    Next

    It seems to only iterate through the top level nodes and not the child nodes.

    -- Jim


  • TheKingKev

     

     

    To recurse through nodes you can call this passing a node.

     

    Dim AL as new Arralist

    Dim NodeCount as Integer

     

    Private Sub SetupNodesForDisplay(ByVal Node As TreeNode)

            AL.Clear()

      NodeCount = 0

            For Each n As TreeNode In Node.Nodes

                RecursiveGetnode(n)

            Next

        End Sub

     

        Private Sub RecursiveGetnode(ByVal n As TreeNode)

            Count +=1

            Al.Add(n.text)

            For Each aNode As TreeNode In n.Nodes

                RecursiveGetnode(aNode)

            Next

        End Sub

     

    A node has a node collection which can be be interogated

     

    Dim A as integer = Node.Nodes.Count ‘For a given level

    The example above collects the total count and a list of names.

     

    There is also the Treeview.Find Method which reguires a node path as an argument.

     



  • Kelly Dyjur

    This is what my example does also, for every TreeNode it calls the method again.


  • How to Find Child Nodes in a TreeView Control