Click here to Skip to main content
15,920,111 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
The fallowing code I output the selected nodes with no problem:
For Each n As TreeNode In GetCheck(TreeView1.Nodes)

builder.Append(n.Text)

Next

Private Function GetCheck(ByVal node As TreeNodeCollection) As List(Of TreeNode)

Dim lN As New List(Of TreeNode)
For Each n As TreeNode In node
If n.Checked Then lN.Add(n)
lN.AddRange(GetCheck(n.Nodes))
Next

Return lN
End Function

My question is how can I distinguish similar nodes when a user clicks on one of them? For example I got the fallowing nodes; if a user click on the second 500 right now my output is 500 what I want is something like 500second so I know it was the second 500 node. Appreciate your help.

300
a
500
a
b
500
a
b
500
a
b
500
a
b
800
a
Posted
Comments
Sergey Alexandrovich Kryukov 24-May-11 16:17pm    
You need to define similarity. What would be the similarity function?
--SA

1 solution

You can use the Treeview.SelectedIndex property to find out exactly which node is selected. If you want another way you could also use the Tag property of the nodes to attach a unique identifier of some kind to each node as well.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 24-May-11 16:20pm    
Marcus, the tag would work but this is not the best way to deal with data stored in the control. The item could be any type; you only need to override ToString on this type as this defines now an instance of this type is presented in the control as an item. In in this item type you can store any data and use it immediately (in event handlers, typically). I posted sample code using this technique many times, not in the mood right now...
--SA
Espen Harlinn 25-May-11 16:31pm    
As Tag is defined as an object it allows you to tie the treeview to an object model, possibly using WeakReference to ensure that the treenodes doesn't keep objects from the model alive beyond their intended lifetime - so I think this is a neat solution. My 5 :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900