|
-
Jan 24th, 2006, 01:14 PM
#1
Thread Starter
New Member
[RESOLVED] Adding a child to root only in a treeview
Hi,
I'm really new at VB. This thread really helped me figure out how to add nodes and rootnodes, but I don't want my treeview to ever go beyond the first level of children (no child added to a child). My first thought was to try and figure out if a child was selected and popup a message box telling the user to choose a rootnode, but now I am leaning toward just selecting the rootnode for the user and adding the child sibling node.
Can anyone point me in the right direction with this?
Thanks!
-
Jan 24th, 2006, 01:49 PM
#2
Re: Adding a child to root only in a treeview
This code will add a Root Node to a treeview and then it will add child nodes every time Button2 is clicked:
VB Code:
Public Class Form1
Dim Selectednode As TreeNode
Private Sub AddChild(ByVal CurrentNode As TreeNode, _
ByVal strText As String)
Dim tvwnode As New TreeNode
tvwnode.Text = strText
CurrentNode.Nodes.Add(tvwnode)
TreeView1.ExpandAll()
End Sub
Private Sub Button1_Click_1(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
'Adds the Root Node
Dim rootnode As TreeNode
rootnode = Me.TreeView1.Nodes.Add("RootNode", "Root")
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
'Adds Children nodes to Root
Selectednode = Me.TreeView1.Nodes(0)
AddChild(Selectednode, "Child")
End Sub
End Class
Regards,
Mark
Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."
-
Jan 24th, 2006, 06:41 PM
#3
Re: Adding a child to root only in a treeview
All you have to do is test the Parent property of a TreeNode. If it is Nothing then it is a root node, otherwise it is a child node. In the AfterSelect event handler of the TreeView, test the parent property of the selected node and disable your Add Node button, or whatever you use, if the node is a child.
-
Jan 25th, 2006, 08:27 AM
#4
Thread Starter
New Member
Re: Adding a child to root only in a treeview
thanks Mark,
That is a step in the right direction for me, but it is only adding children to the original root. I probably wasn't clear that I will have several root nodes. I see where I can use this though, I just need a way to figure out what level my rootnodes are for Selectednode = Me.TreeView1.Nodes(0).
-
Jan 25th, 2006, 08:50 AM
#5
Hyperactive Member
Re: Adding a child to root only in a treeview
Each treenode has it's own childnodes. All you have to do to add nodes to the treeview rootnode is:
VB Code:
TreeView1.Nodes(0).Nodes.Add("My New Node")
This will add the node to the rootnode, first node of the treeview, no matter what node is selected. Just incriment the index everytime you need to add it to a different rootnode.
I hope this helps.
Currently Using: VS 2005 Professional
-
Jan 25th, 2006, 08:58 AM
#6
Thread Starter
New Member
Re: Adding a child to root only in a treeview
jmcilhinney,
that did it for me, thank you.
here is what I ended up with...using a message box. I like the idea of disabling the "add child" button, but for now I'm cooking with gas.
VB Code:
'handles the button click for adding a new ditch section to the tree view.
Private Sub cmdSectAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSectAdd.Click
'// add some items
If (Not tvwDitch.SelectedNode.Parent Is Nothing) Then
MessageBox.Show("Please select a Ditch to add Section to, and try again.", "Select a Ditch", MessageBoxButtons.OK)
Else
AddChild(Selectednode, "Section1")
End If
End Sub
Private Sub AddChild(ByVal CurrentNode As TreeNode, _
ByVal strText As String)
Dim tvwnode As New TreeNode
With tvwnode
.Text = strText
End With
CurrentNode.Nodes.Add(tvwnode)
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|