Click here to Skip to main content
15,921,463 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi readers,

I'm new to VB. Create a fucntion to loop all nodes(parents & child) to check its string. This is what I need to do.

My tree will look something like below(tree parent/child can be random)

Level1
Level2
Level3
Level2
Level3
Level3
Level2
Level1
Level2
Level2

Hope you guys understand the above tree :)

I tried to do recusive loop and I got stuck. Because once the 1st level3 is done, it does not continue with the next sibling(next of Level2) but going back to the next sibling of Level1.

Do you guys have the best way to loop this?loop a dynamic tree?

Thanks.
Posted
Comments
Dave Kreskowiak 19-Nov-10 17:04pm    
Without seeing YOUR code, it's impossible to tell you where YOU went wrong. I object to the person posting HIS code in an attempt to answer your question. You won't learn anything that way.

1 solution

Try this

VB
Function GetTreeNodesString(ByVal parent As TreeView)
        Dim nodesName As String = ""
        For Each root As TreeNode In parent.Nodes
            nodesName = nodesName & "+" & root.Text & Environment.NewLine
            nodesName = nodesName & GetChildNodesString(root, 1)
        Next
        Return nodesName
    End Function

    Function GetChildNodesString(ByVal childNode As TreeNode, ByVal level As Integer) As String
        Dim nodesName As String = ""
        Dim spacer As String = "  "
        For i As Integer = 0 To level - 1
            spacer &= spacer
        Next
        For Each node As TreeNode In childNode.Nodes
            nodesName = nodesName & spacer & "+" & node.Text & Environment.NewLine
            nodesName = nodesName & GetChildNodesString(node, level + 1)
        Next
        Return nodesName
    End Function
 
Share this answer
 
Comments
skunkhead 22-Dec-10 23:20pm    
thanks qontary

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900