strange problem For Each loop in Treeview
Peter,
I can see the point you are making.
My trees are not big, so speed is not really an issue. If I could get your
code to work I would use it however.
Now have you tried your code on a tree with exactly the structure I posted
last?
I can't really see why there should be something funny with my tree
structure. A treeview is a treeview and there is not that much to it apart
from the normal parameters such as index, key, text etc.
My trees are generated in a really complicated and big Userform, that is
part of an Excel add-in. It is an interface to search an Interbase database.
If you want I can send you the zipped add-in, which is about 350 kB.
Will have a look again at your code and try to figure out why it trips up.
Regards, 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
|