LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 70
Default strange problem For Each loop in Treeview

Hi,

I think the problem might be that your statement:

For Each nodChild in MainForm.TreeView1.Nodes

is wrong. This will direct the routine to start at the top of the tree each
time. I would imagine you want something like:

Option Explicit
Dim sItems() As String
Dim nItem As Long

Private Sub Command1_Click()
Dim i As Long

nItem = 0
IterateTreeView tv1.Nodes(1)

For i = 0 To UBound(sItems)
Debug.Print sItems(i)
Next i
End Sub

Private Sub Form_Load()
' Fill the tree for testing purposes
Dim X As Node, Y As Node

Set X = tv1.Nodes.Add(, , "N1", "N1")
Set Y = tv1.Nodes.Add(X, tvwChild, "N11", "N11")

tv1.Nodes.Add Y, tvwChild, "N111", "N111"
tv1.Nodes.Add Y, tvwChild, "N112", "N112"

Set X = tv1.Nodes.Add(, , "N2", "N2")
tv1.Nodes.Add X, tvwChild, "N21", "N21"
tv1.Nodes.Add X, tvwChild, "N22", "N22"
End Sub

Sub IterateTreeView(a_oNode As Node)
Dim X As Node

ReDim Preserve sItems(nItem) As String
sItems(nItem) = a_oNode.Text

Set X = a_oNode.Child
If Not X Is Nothing Then
nItem = nItem + 1
IterateTreeView X
End If

Set X = a_oNode.Next
Do While Not X Is Nothing
nItem = nItem + 1
IterateTreeView X
Set X = X.Next
Loop
End Sub

HTH

Peter Beach

"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




 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
strange problem -Bryan[_2_] Excel Discussion (Misc queries) 3 May 1st 08 06:56 PM
Strange VLOOKUP Problem DMDave Excel Discussion (Misc queries) 6 May 8th 06 02:06 AM
Strange Problem Perry Excel Discussion (Misc queries) 7 April 12th 06 09:52 PM
Strange Problem... Sujesh Excel Discussion (Misc queries) 6 December 30th 05 02:56 PM
strange problem For Each loop in Treeview RB Smissaert Excel Programming 0 August 25th 03 07:18 PM


All times are GMT +1. The time now is 04:13 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"