Test is a TreeView node exists?

ScoobyChris

Member
Joined
Sep 3, 2006
Messages
6
Programming Experience
1-3
Hi,

A simple question, I hope!!

I'm building a treeview at runtime and I need a method of testing if a node already exists before creating it. If I create a simple node using...

TreeView1.Nodes.Add("Test Node")

How can I tell if this node exists? Something like...

If TreeView.Nodes.Contains("Test Node") then do this

Thanks in advance! :)

 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,438
Location
Norway
Programming Experience
10+
Check out this cool Treeview search routine (posted yesterday in this forum :)), http://www.vbdotnetforums.com/showpost.php?p=39625&postcount=3 it searches Text property (exact or part) and supports recursive search of childs node collections.

You could also use the Find method of the Nodes collection in a Treeview, it also supports recursion for child collections but it will only search exact text for Name property of each TreeNode (the Name property is the 'key'). Note that for the question you asked for method Add("node text") only sets the Text, not Name.
 

ScoobyChris

Member
Joined
Sep 3, 2006
Messages
6
Programming Experience
1-3
Hi John,

I ended up finding a very similar post on a forum (might have been this one actually. My code now looks as below. Obviously there's a lot of stuff in there that will make no sense at all but I think you'll get the general gist of what's goint on.

The idea is that if a parent node already exists then a child value is created under that parent instead (The formatting is all to pot on this by the way :)!)
VB.NET:
[SIZE=2][COLOR=#008000]'add parent node if it doesn't already exist (if so add quantity as a child)[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] parentNode [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] TreeNode [/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2] TreeView1.Nodes[/SIZE]
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] parentNode.Text = ds.Tables([/SIZE][SIZE=2][COLOR=#800000]"pub_name"[/COLOR][/SIZE][SIZE=2]).Rows(0)([/SIZE][SIZE=2][COLOR=#800000]"name"[/COLOR][/SIZE][SIZE=2]) [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'add requirement as child of parent node[/COLOR][/SIZE]
[SIZE=2]TreeView1.Nodes(parentNode.Index).Nodes.Add(label & [/SIZE][SIZE=2][COLOR=#800000]" = "[/COLOR][/SIZE][SIZE=2] & pub(1)) _[/SIZE]
[SIZE=2].Tag = column & [/SIZE][SIZE=2][COLOR=#800000]"*"[/COLOR][/SIZE][SIZE=2] & pub(0) & [/SIZE][SIZE=2][COLOR=#800000]"&"[/COLOR][/SIZE][SIZE=2] & pub(1)[/SIZE]
[SIZE=2][COLOR=#008000]'add on to total drops[/COLOR][/SIZE]
[SIZE=2]total_drops = total_drops + pub(1)[/SIZE]
[SIZE=2]added_as_child = [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Next[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'Check if requirement was added as a child[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] added_as_child = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'was not so now create new parent and add requirement as child[/COLOR][/SIZE]
[SIZE=2]TreeView1.Nodes.Add(ds.Tables([/SIZE][SIZE=2][COLOR=#800000]"pub_name"[/COLOR][/SIZE][SIZE=2]).Row(0)([/SIZE][SIZE=2][COLOR=#800000]"name"[/COLOR][/SIZE][SIZE=2])).Nodes.Add(column, label & [/SIZE][SIZE=2][COLOR=#800000]" = "[/COLOR][/SIZE][SIZE=2] & pub(1)) _[/SIZE]
[SIZE=2].Tag = column & [/SIZE][SIZE=2][COLOR=#800000]"*"[/COLOR][/SIZE][SIZE=2] & pub(0) & [/SIZE][SIZE=2][COLOR=#800000]"&"[/COLOR][/SIZE][SIZE=2] & pub(1)[/SIZE]
[SIZE=2][COLOR=#008000]'add on to total drops[/COLOR][/SIZE]
[SIZE=2]total_drops = total_drops + pub(1)[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'clear ds row[/COLOR][/SIZE]
[SIZE=2]ds.Tables([/SIZE][SIZE=2][COLOR=#800000]"pub_name"[/COLOR][/SIZE][SIZE=2]).Rows.Clear()[/SIZE]
[SIZE=2][COLOR=#0000ff]Next[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
 
Last edited by a moderator:
Top Bottom