VB6 TreeView Node "Key" property Equivalent in VB.NET

D

Don Wash

Hi There!

I'm using VB.NET to create a TreeView application and unfortunately I could
not find "Key" property in Node items of the TreeView.

We used to have "Key" property in TreeView node object in VB6.

What is the equivalent of "Key" property of TreeView node in VB.NET? Has the
property name "Key" has been changed to something else? Or VB.NET does not
support the "Key" property anymore? If so what are the alternatives?

What I need is I need to assign "Key" (which contains text and number) to
each node I add to the TreeView. So "Index" property won't help me. I need
something similar to "Key" property that is used to support in TreeView
Nodes of VB6.

Thanks all in advance!
Don
 
H

Herfried K. Wagner [MVP]

* "Don Wash said:
I'm using VB.NET to create a TreeView application and unfortunately I could
not find "Key" property in Node items of the TreeView.

There is no direct replacement for this property. You can set up a
'Hashtable' and store your (key, 'TreeNode') pairs for fast lookup by a
key there.
 
A

André Nogueira

lol seems I'm not the only one with problems...
I'm looking into your solution as we speak.

André Nogueira
 
D

Don Wash

Thanks, I've tried HashTable method but it doesn't seems to work.

Here's the code...

With objNewNode
htMain.Add(strNodeKey, .Index)
htMain_IndexToKey.Add(.Index, strNodeKey)

.Text = "Node Item " & i
.ImageIndex = 1
End With

.Nodes.Add(objNewNode)

htMain and htMain_IndexToKey are HashTable objects. What happens is that
Index of objNewNode is always '0' (Zero).

So I though objNewNode is an independent TreeView Node which hasn't been
associated with the real TreeView control and I tried this code instead...

Dim intNodeIndex as Integer = .Nodes.Add(objNewNode)

htMain.Add(strNodeKey, intNodeIndex )
htMain_IndexToKey.Add(intNodeIndex , strNodeKey)

and still intNodeIndex equal '0' every time. How can I get the actual Node's
index currently being added to the TreeView Nodes collection?

Thanks again!

Don
 
D

Don Wash

Hi André,

How are you getting at? Did you get it to work?

Cheers,
Don

André Nogueira said:
lol seems I'm not the only one with problems...
I'm looking into your solution as we speak.

André Nogueira
 
J

Jay B. Harlow [MVP - Outlook]

Don,
Look carefully at how the TreeView Nodes are owned.

The TreeView itself only owns the root nodes, with indexes from 0 to however
many root nodes.

Each root node will own their children, their children will have indexes
from 0 to however many children each respective root has.

The grand children (of root) will have indexes from 0 to however many
children each parent (child of root) has respectively.

And so on.

In other words all the nodes in the following will have an index of 0,
except Grand Child 2 & Root 2 which will have indexes of 1.

Root 1 (index = 0)
Child 1 (index = 0)
Grand Child 1 (index = 0)
Great Grand Child 1 (index = 0)
Grand Child 2 (index = 1)
Great Grand Child 2 (index = 0)
Child 2 (index = 0)
Grand Child 3 (index = 0)
Great Grand Child 3 (index = 0)
Root 2 (index = 1)
Child 3 (index = 0)
Grand Child 4 (index = 0)
Great Grand Child 4 (index = 0)

If you need to index the nodes by both Key & Index, where Index is across
all nodes in the tree) I would recommend a
System.Collections.Specialized.NameValueCollection instead of a HashTable.
As the NameValueCollection supports indexing by both Key & Index.

Hope this helps
Jay

Don Wash said:
Thanks, I've tried HashTable method but it doesn't seems to work.

Here's the code...

With objNewNode
htMain.Add(strNodeKey, .Index)
htMain_IndexToKey.Add(.Index, strNodeKey)

.Text = "Node Item " & i
.ImageIndex = 1
End With

.Nodes.Add(objNewNode)

htMain and htMain_IndexToKey are HashTable objects. What happens is that
Index of objNewNode is always '0' (Zero).

So I though objNewNode is an independent TreeView Node which hasn't been
associated with the real TreeView control and I tried this code instead...

Dim intNodeIndex as Integer = .Nodes.Add(objNewNode)

htMain.Add(strNodeKey, intNodeIndex )
htMain_IndexToKey.Add(intNodeIndex , strNodeKey)

and still intNodeIndex equal '0' every time. How can I get the actual Node's
index currently being added to the TreeView Nodes collection?

Thanks 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