Process: A node is added in a treeview control with node text = textbox1.text
I want to prevent addition of duplicate node i.e. say, if a node with text “ABC” is added then next time, a node with text “ABC” should not be added to treeview control.
I tried following methods but could not achieve desired result.
Method A)
Dim list As New ArrayList
list.Add(TextBox1.Text)
if list.Contains(Textbox1.Text) then
MsgBox("Use different name")
else
.....code to add node with text
end if
Method B)
if Treeview1.Nodes.Count > 0 then
For i = 0 to Treeview1.Nodes.Count
if Treeview1.Nodes(i).Text=Textbox1.Text then
MsgBox("Use different name")
end if
next
else
........code to add node with text
end if
I could not understand the solutions suggested for C# on this forum.
Any help will be really appreciated.
Thanks
Method A should work OK. You might have another error in your code (in the else section?).
listshould be declared static if it is in a function that’s being called repeatedly, else it will be reset to new (cleared) each time.Method B has a couple of errors: (1) the for statement should be
For i = 0 to Treeview1.Nodes.Count - 1(maybe use “for each”), and theelsewith the code to add the node should be after themsgboxstatement. Also, method B only search for the root node(s) of the treeview. You would need to traverse the tree to check all the nodes.