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

Peter,

Had another look at your code.
Looks to me it is just wrong. Corrected it and it now works fine.


You gave this for the recursive 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


But it should be this:

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

If Not X Is Nothing Then
nItem = nItem + 1
IterateTreeView X
End If

End Sub


Hope this settles it.


RBS


"Peter Beach" wrote in message
...
Hi,

Well mine ran OK on a tree that went down as far as that.

I can't explain why it shouldn't work on the tree you specify. I have

never
had the problems you are having. Would you like to send me the code you

are
using to populate the treeview. It maybe that there is something funny in
its structure.

To return to my earlier point though, look again at your code (slightly
simplified):

Sub Tester(ByRef nodcurrent As Node)
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
debug.print nodchild.text
Tester nodChild
End If
End If
Next nodChild
End If
End Sub

Now you iterate through potentially every node in the treeview trying to
find a node whose parent is nodCurrent. But you don't need to do that.
Easier, and quicker surely, to examine each node and then look for its
children and siblings? Trust me, that For Each nodChild loop is

potentially
going through the entire treeview looking for a cell whose parent is
nodCurrent and is doing it each time the routine is called.

That's not necessary. The whole idea behind a linked list (which is what

a
treeview control represents graphically) is that from the top of the tree
you can navigate to any point in the tree without every having to know the
structure of the entire list. The .Child and .Next property (together

with
the .Parent and .Previous properties) allow you to traverse the entire
structure.

Regards,

Peter Beach

"RB Smissaert" wrote in message
...
Peter,

The tree I used was actually one level deeper, like this:

R
_N
_ _N
_ _N
_ _ _N
_ _ _ _ N
_ _ _ _ N
_ _ _ _ _ N
_ _ _ _ N
_ _ _ _ N
_ _ _ _ _ N
_ _ _ _ _ _ N

I have tried your code on a few more tree structures and it works fine,

but
not on this one.
Hopefully you are not getting too bored with this.


RBS


"Peter Beach" wrote in message
...
Hi,

Odd. My code worked fine for me.

The point I was making was that I can't see the purpose of the lines:

For Each nodChild in MainForm.TreeView1.Nodes
If Not nodChild.Parent Is Nothing Then
If nodChild.Parent = nodcurrent Then

which would seem to iterate through the tree until the target node is
reached. But you already know what the target node is - it's either

the
first node (for the initial call) or the it's the node currently being
inspected, so it seems to me that you don't need to go iterating

through
the
tree looking for it. You can from the node recursively call the

routine
for
the first child and then iterate through all its siblings.

However programming is that art of achieving results, if your code

works,
stick with it.

Regards,

Peter Beach

"RB Smissaert" wrote in message
...
Peter,

Given your code a try, but it doesn't work.
UBound(sItems) gives 67000, which I presume is the limit of what the

array
can hold and it gets into an endless loop.
The code I use now is actually very efficient as it passes every

node
only
once. Have you tried both ways?

RBS


"Peter Beach" wrote in message
...
Hi,

Glad you got it worked out. I'm still a bit puzzled by your code,

as
you
seem to iterate through the entire tree each time, searching for

your
target
node whereas it is surely more efficient simply to work down and

across
from
the target node. The code I posted earlier only traverses the tree

a
single
time which would seem to be more efficient.

Regards,

Peter Beach

"RB Smissaert" wrote in message
...
Peter,

The Sub is fine. It was just the typo as stated in my previous

post.

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 08:10 PM.

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

About Us

"It's about Microsoft Excel"