问题描述
您好,读者,
我是VB新手.创建一个功能以循环所有节点(父级和子级)以检查其字符串.这是我需要做的.
我的树如下图所示(树的父级/子级可以是随机的)
Level1
Level2
Level3
Level2
Level3
Level3
Level2
Level1
Level2
Level2
希望你们能理解上面的树:)
我尝试进行回溯循环,但被卡住了.因为一旦完成第一个level3,它就不会继续下一个同级(Level2的下一个),而是返回到Level1的下一个同级.
你们有没有最好的循环方法?循环动态树?
谢谢.
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.
推荐答案
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
这篇关于如何遍历所有节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!