ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   strange problem For Each loop in Treeview (https://www.excelbanter.com/excel-programming/275233-re-strange-problem-each-loop-treeview.html)

RB Smissaert

strange problem For Each loop in Treeview
 
Correcting a simple typo solved this problem:

If nodChild.Parent = nodcurrent Then
should be:
If nodChild.Parent Is nodcurrent Then


RBS


"RB Smissaert" wrote in message
...
Use a recursive Sub to traverse the nodes of a Treeview control from top

to
bottom.
Noticed that there is a problem if 2 nodes in the Treeview have the same
text.
Rather than ending at the lowest node the sub returns to the higher node
with the same text and the Sub doesn't exit.

This are the 2 Subs that demonstrate it:

Sub Tester(ByRef nodcurrent As Node, _
ByVal intdepth As Integer)

Dim nodChild As Node

If nodcurrent.Children 0 Then
For Each nodChild In MainForm.TreeView1.Nodes
If Not nodChild.Parent Is Nothing Then
If nodChild.Parent = nodcurrent Then

MsgBox nodChild.Text

Call Tester(nodChild, (intdepth + 1))
End If
End If
Next nodChild
End If

End Sub


Sub DoTester()

Tester MainForm.TreeView1.Nodes(1), 0

End Sub


Is this a known bug, or am I doing something wrong?
One way to solve this problem is to fill an array with the indeces of the
visited nodes and make sure the same node doesn't get visited twice, but
perhaps there is a better solution for this.
Thanks for any advice.


RBS





All times are GMT +1. The time now is 01:47 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com