[RESOLVED] Adding a Node to a Child Node - How?
I am adding a child node (Node 1) to a treeview and then I want to add a Child Node (Node 2) to the newly added child node (Node 1):
VB Code:
AddChild(Selectednode, "John Doe", 12) 'Adding Node1
intNodeNum = TreeView1.Nodes(0).Nodes.Count + 1 ' I am attempting to get the
' index of last added node
Selectednode = TreeView1.Nodes(0).Nodes(intNodeNum) ' <== I get an error on this line
AddChild(Selectednode, "Address", 13) ' <==Node 2
How do I do this?
Re: Adding a Node to a Child Node - How?
I tried using the following:
VB Code:
TreeView1.SelectedNode = TreeView1.Nodes(0).PrevNode
Thinking that it would given me the index of the last node added (Node 1) but no dice.
Re: Adding a Node to a Child Node - How?
Quote:
Originally Posted by Mark Gambo
I am adding a child node (Node 1) to a treeview and then I want to add a Child Node (Node 2) to the newly added child node (Node 1):
VB Code:
AddChild(Selectednode, "John Doe", 12) 'Adding Node1
intNodeNum = TreeView1.Nodes(0).Nodes.Count + 1 ' I am attempting to get the
' index of last added node
Selectednode = TreeView1.Nodes(0).Nodes(intNodeNum) ' <== I get an error on this line
AddChild(Selectednode, "Address", 13) ' <==Node 2
How do I do this?
Hi Mark,
Why you want to do it by code?
If you go via TOOLBOX -> select TREEVIEW and create it on your Form,
then go to the properties of your TREEVIEW and select NODES and you can
Add Root and Add Cilds as many as you need.
Happy New Year,
sparrow1
Re: Adding a Node to a Child Node - How?
Quote:
Originally Posted by sparrow1
Hi Mark,
Why you want to do it by code?
If you go via TOOLBOX -> select TREEVIEW and create it on your Form,
then go to the properties of your TREEVIEW and select NODES and you can
Add Root and Add Cilds as many as you need.
Happy New Year,
sparrow1
Happy New Year to you!!!
I want to be able to add the nodes during run time, specificaly when a User adds a name to a particular node (Node 1) I want to automatically add a child node to the newly created node (Node 2).
Thanks!
Re: Adding a Node to a Child Node - How?
I thought so after I saw your second thread about Treeviews.
I'll try it.
Wkr,
sparrow1
Re: Adding a Node to a Child Node - How?
Quote:
Originally Posted by sparrow1
I thought so after I saw your second thread about Treeviews.
I'll try it.
Wkr,
sparrow1
Thanks
Re: Adding a Node to a Child Node - How?
Why don't you have the AddChild method return a reference to the newly added node? That way you can simply pass that reference as the first argument to the next call to AddChild.
As to your error, the index of the last node is Count-1, not Count+1.
Re: Adding a Node to a Child Node - How?
Quote:
Originally Posted by jmcilhinney
Why don't you have the AddChild method return a reference to the newly added node? That way you can simply pass that reference as the first argument to the next call to AddChild.
As to your error, the index of the last node is Count-1, not Count+1.
Thanks for the post. The following code is my AddChild Sub:
VB Code:
Private Sub AddChild(ByVal CurrentNode As TreeNode, _
ByVal strText As String, ByVal intImage As Integer, _
Optional ByVal intSelectImage As Integer = 99999)
Dim tvwnode As New TreeNode
With tvwnode
.Text = strText
.ImageIndex = intImage
If intSelectImage <> 99999 Then .SelectedImageIndex = intSelectImage Else .SelectedImageIndex = intImage
End With
CurrentNode.Nodes.Add(tvwnode)
TreeView1.ExpandAll()
End Sub
How would I alter this sub in order to return a reference to the newly added node?
Thanks!
Re: Adding a Node to a Child Node - How?
VB Code:
Private [b]Function[/b] AddChild(ByVal CurrentNode As TreeNode, _
ByVal strText As String, ByVal intImage As Integer, _
Optional ByVal intSelectImage As Integer = 99999) [b]As TreeNode[/b]
Dim tvwnode As New TreeNode
With tvwnode
.Text = strText
.ImageIndex = intImage
If intSelectImage <> 99999 Then .SelectedImageIndex = intSelectImage Else .SelectedImageIndex = intImage
End With
CurrentNode.Nodes.Add(tvwnode)
'personally I would put this outside of this function
'[b]TreeView1.ExpandAll()[/b]
'return value
Return tvwnode
End Sub
Make a function out of it.
Re: Adding a Node to a Child Node - How?
Thanks, it worked like a charm!!