Question Treeview unique branch text

jimirayyng

Member
Joined
Apr 22, 2009
Messages
8
Location
hampshire uk
Programming Experience
5-10
Hi, This is my first post, please be gentle....:)

I Have a treeview that has its nodes populated by clicking on a row in a grid, i keep stuff like the record key in the node.tags and build the node text from the fields in the record (I obtained the data from the db earlier). Any amount of nodes and sub nodes can be added. i use recursive functions to save the tree and reload it all this is working fine. Ok, on to my question.

I would like to stop duplicate nodes on the same branch(ie nodes with the same text) being added. duplicates can only be legall if added on a different branch.

I thought about using the .Fullpath attribute and seaching for duplicates, but am not sure if there is an easier more elegant way than this.

I dont expect a complete solution, just a few pointers(no pun intended)

Many Thanks,

JRY

ps great forum !!!
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,471
Location
Norway
Programming Experience
10+
Get the root node and use the Find method of its Nodes collection. Use same Name (key) for nodes as Text. Here's also a sample function that works for this purpose, the key and any node reference of that branch must be supplied:
VB.NET:
Function BranchKeyExists(ByVal key As String, ByVal node As TreeNode) As Boolean
    Do Until node.Level = 0
        node = node.Parent
    Loop
    Return node.Name = key OrElse node.Nodes.Find(key, True).Length > 0
End Function
 
Top Bottom