Results 1 to 3 of 3

Thread: how to recursively view all child nodes in a treeview

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2002
    Posts
    7

    how to recursively view all child nodes in a treeview

    Hi,

    I'm trying to recursively go through all of the child nodes in a tree view upon clicking a button but I'm stuck. Currently, the code below only works for the top nodes in my treeview but I want it to go through each of these nodes' children, and their children and so on.


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim child As TreeNode
    Dim fullpath As String


    For Each child In TreeView1.Nodes
    fullpath = child.FullPath
    Next

    End Sub

    If anyone can suggest a solution, or where I'm going wrong I'd be grateful.

    Thanks,

    Karl

  2. #2
    Registered User
    Join Date
    Nov 2002
    Location
    Växjö, Sweden
    Posts
    314
    Maybe there is an easier way but this code displays a Messagebox with the full path for each node in a treeview. I had a TreView1 with a couple of nodes and a Button1 control:

    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click
    
        Dim node As TreeNode
        For Each node In TreeView1.Nodes
            MsgBox(node.FullPath)
            GoThroughNodes(node)
        Next
    
    End Sub
    
    Private Sub GoThroughNodes(ByVal node As TreeNode)
    
        Dim node2 As TreeNode
        For Each node2 In node.Nodes
            If node2.GetNodeCount(False) = 0 Then
                MsgBox(node2.FullPath)
            Else
                MsgBox(node2.FullPath)
                GoThroughNodes(node2)
            End If
        Next
    
    End Sub
    I recursively call the GoThroughNodes procedure to go into each nodes children.

    /Leyan

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2002
    Posts
    7

    Thumbs up Thanks

    Thanks again Leyan for your help.

    That works perfectly!

    Karl

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width