Baffeled by Treeview Nodes Index!!!!!

D

Don

I thought each Treeview node had a unique index in the nodes
collection but apparently I was wrong.

I'm pretty sure the VB6 Treeview had unique node indexs but I don't
want to set up an old computer with VB6 installed to check it.

Please try the following tree structure and tell me if I'm nuts:

Families
----Jones
--------James
------------Frank
----Smith
--------Willie
------------Bob
------------Lary

==================
1. Put a Treeview and a Listbox on a form.

2. Name the Treeview TV and paste the following code in the
appropriate events

3. Click the tree nodes and observe the index of each.

I understand that each node has its own nodes collection but is there
a unique identifier for every node? If so, how do you get it. Without
a unique index it seems to lead to some whacky results if you do the
following:

tv.Nodes.Remove(tv.SelectedNode)

WHAT AM I MISSING?

Thanks,

Don

Private Sub frmTest_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim nod As TreeNode, wnod As TreeNode

nod = tv.Nodes.Add("Families")
nod = nod.Nodes.Add("Jones")
nod = nod.Nodes.Add("James")
nod = nod.Nodes.Add("Frank")
nod.EnsureVisible()

nod = tv.Nodes(0).Nodes.Add("Smith")
wnod = nod.Nodes.Add("Willie")
nod = wnod.Nodes.Add("Bob")
nod = wnod.Nodes.Add("Larry")
nod.EnsureVisible()

nod = tv.Nodes(0).Nodes.Add("Johnson")
nod.EnsureVisible()
'tv.HideSelection = False
End Sub


Private Sub tv_AfterSelect(ByVal sender As System.Object, ByVal e
As System.Windows.Forms.TreeViewEventArgs) Handles tv.AfterSelect

Dim n As TreeNode = tv.SelectedNode
On Error Resume Next
With ListBox1
.Items.Clear()
.Items.Add("Index: " & n.Index.ToString)
.Items.Add("Text: " & n.Text)
.Items.Add("Kids: " & n.GetNodeCount(True))
.Items.Add("Parent: " & n.Parent.Index)
End With
End Sub
 
A

Armin Zingler

Don said:
I thought each Treeview node had a unique index in the nodes
collection but apparently I was wrong.

I'm pretty sure the VB6 Treeview had unique node indexs but I don't
want to set up an old computer with VB6 installed to check it.

Please try the following tree structure and tell me if I'm nuts:

Families
----Jones
--------James
------------Frank
----Smith
--------Willie
------------Bob
------------Lary

==================
1. Put a Treeview and a Listbox on a form.

2. Name the Treeview TV and paste the following code in the
appropriate events

3. Click the tree nodes and observe the index of each.

I understand that each node has its own nodes collection but is
there a unique identifier for every node? If so, how do you get it.
Without a unique index it seems to lead to some whacky results if
you do the following:

tv.Nodes.Remove(tv.SelectedNode)

WHAT AM I MISSING?

Thanks,

Don


I didn't test the code, but if you need to identify a node, store a
reference to the node.


Armin
 
D

Don

In my example all nodes have an index of 0 except "Smith" and "Larry"
which each have an index of 1. Each sub tree seems to have its own 0
based indexing. The control must have been designed that way for some
purpose. My question is . . . what purpose and how/when would I make
use of it?

I'm a long time VB programmer who's been away from it for quite a
while and am trying to get my noggin around VB.Net. (I think this is
try #3) This treeview thing is one of many that's befuddling me.

I feel like one of those natives in the movies that see a cigarette
lighter for the first time. Mundane if you're hip but magic if you're
not. I'm, aparantly, not. :(

Thanks,

Don
 
A

Armin Zingler

Don said:
In my example all nodes have an index of 0 except "Smith" and
"Larry" which each have an index of 1. Each sub tree seems to have
its own 0 based indexing. The control must have been designed that
way for some purpose. My question is . . . what purpose and how/when
would I make use of it?

I should have tested it... I'm gonna do that now.


Armin
 
A

Armin Zingler

Don said:
In my example all nodes have an index of 0 except "Smith" and
"Larry" which each have an index of 1. Each sub tree seems to have
its own 0 based indexing. The control must have been designed that
way for some purpose. My question is . . . what purpose and how/when
would I make use of it?

I'm a long time VB programmer who's been away from it for quite a
while and am trying to get my noggin around VB.Net. (I think this is
try #3) This treeview thing is one of many that's befuddling me.

I feel like one of those natives in the movies that see a cigarette
lighter for the first time. Mundane if you're hip but magic if
you're not. I'm, aparantly, not. :(


Well, I tested it now, but everything works as expected. Each node has it's
sub nodes. They are stored in a collection. The index is the index within
this collection. For example, you can use the index to persistently store a
reference to a node outside the application, like storing "2,0,1" in a file
to store the user's selection when the program quits and restore it at
restart. So, I don't see a problem - or I didn't get the point.



Armin
 
D

Don

Hi Armin,

So, you're saying that the only intrinsic way to address a node is
through "relative addressing". It appears that is apparently true but
seems very inelegant and messy. At least it seems that way to me.

Thank you for your help.

Don
 
A

Armin Zingler

Don said:
Hi Armin,

So, you're saying that the only intrinsic way to address a node is
through "relative addressing". It appears that is apparently true
but seems very inelegant and messy. At least it seems that way to
me.


The other way to address a node is to store the reference.


Armin
 
D

Don

I've extended the treenode class to add some new properties. It seems
to work well. I'm also thinking about trying to extend the nodes
collection. This is my first attempt and am wondering if you have any
suggestions or potential gotcha's I might prepare for.

Thank you again,

Don
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top