Results 1 to 6 of 6

Thread: [RESOLVED] Adding a child to root only in a treeview

  1. #1

    Thread Starter
    New Member Golfball's Avatar
    Join Date
    Jan 2006
    Location
    Central Pennsylvania
    Posts
    3

    Resolved [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!

  2. #2
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    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:
    1. Public Class Form1
    2.     Dim Selectednode As TreeNode
    3.  
    4.     Private Sub AddChild(ByVal CurrentNode As TreeNode, _
    5.                        ByVal strText As String)
    6.         Dim tvwnode As New TreeNode
    7.         tvwnode.Text = strText
    8.         CurrentNode.Nodes.Add(tvwnode)
    9.         TreeView1.ExpandAll()
    10.     End Sub
    11.  
    12.     Private Sub Button1_Click_1(ByVal sender As System.Object, _
    13.                 ByVal e As System.EventArgs) Handles Button1.Click
    14.         'Adds the Root Node
    15.         Dim rootnode As TreeNode
    16.         rootnode = Me.TreeView1.Nodes.Add("RootNode", "Root")
    17.     End Sub
    18.  
    19.     Private Sub Button2_Click(ByVal sender As System.Object, _
    20.                 ByVal e As System.EventArgs) Handles Button2.Click
    21.         'Adds Children nodes to Root
    22.         Selectednode = Me.TreeView1.Nodes(0)
    23.         AddChild(Selectednode, "Child")
    24.     End Sub
    25. 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."


  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    New Member Golfball's Avatar
    Join Date
    Jan 2006
    Location
    Central Pennsylvania
    Posts
    3

    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).

  5. #5
    Hyperactive Member
    Join Date
    May 2005
    Posts
    258

    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:
    1. 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

  6. #6

    Thread Starter
    New Member Golfball's Avatar
    Join Date
    Jan 2006
    Location
    Central Pennsylvania
    Posts
    3

    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:
    1. 'handles the button click for adding a new ditch section to the tree view.
    2.     Private Sub cmdSectAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSectAdd.Click
    3.         '// add some items
    4.         If (Not tvwDitch.SelectedNode.Parent Is Nothing) Then
    5.             MessageBox.Show("Please select a Ditch to add Section to, and try again.", "Select a Ditch", MessageBoxButtons.OK)
    6.         Else
    7.             AddChild(Selectednode, "Section1")
    8.         End If
    9.     End Sub
    10.  
    11.     Private Sub AddChild(ByVal CurrentNode As TreeNode, _
    12.                    ByVal strText As String)
    13.         Dim tvwnode As New TreeNode
    14.         With tvwnode
    15.             .Text = strText
    16.         End With
    17.         CurrentNode.Nodes.Add(tvwnode)
    18.     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
  •  



Click Here to Expand Forum to Full Width