[RESOLVED] Finding Node in Treeview - How?
I am attempting to find a particular node in a Treeview so that I can add nodes to that particular node. But when I try to loop through the nodes I am only getting the Root node. Here is the code I am using:
VB Code:
Private Sub AddNodes()
Dim tnode As TreeNode 'Treeview Node
Dim a As Integer 'Counter
tnode = Me.TreeView1.Nodes.Add("Root Node", "Root", 14)
Selectednode = tnode
AddChild(Selectednode, "Child 1", 1)
AddChild(Selectednode, "Child 2", 2)
AddChild(Selectednode, "Child 3", 3)
AddChild(Selectednode, "Child 4", 4)
AddChild(Selectednode, "Child 5", 5)
AddChild(Selectednode, "Child 6", 6)
a = 0
With TreeView1
For Each tnode In .Nodes '<==The root node is the only node in collection
With .Nodes
If .Item(a).Text = "Child 3" Then 'Add Gr Child Node to Child 3
Selectednode = .Item(a)
AddChild(Selectednode, "Grand Child 1", 12)
AddChild(Selectednode, "Grand Child 2", 13)
Exit For 'Exit Loop if Found
End If
End With
a = a + 1 'Increment Counter
Next
End With
End Sub
Any ideas?
Re: Finding Node in Treeview - How?
I figured it out just as I was pressing the submit button. I need to change the scope. Here is the code:
VB Code:
With TreeView1.Node(0) '<===I needed to add the Nodes(0)
For Each tnode In .Nodes 'Num of nodes attached to root node collection
With .Nodes
If .Item(a).Text = "Child 3" Then 'Add Gr Child Node to Child 3
Selectednode = .Item(a)
AddChild(Selectednode, "Grand Child 1", 12)
AddChild(Selectednode, "Grand Child 2", 13)
Exit For 'Exit Loop if Found
End If
End With
a = a + 1 'Increment Counter
Next
End With