Solved

Adding children to a treeview after the main nodes are created

Posted on 2004-09-24
3
330 Views
Last Modified: 2010-04-23
Is there a way to add children to the main nodes after the main nodes are already in the treeview? For instance the treeview looks like this upon load:

1
2
3
4
5

On a button click, I want to add children to certain nodes:

1
2
  1
  2
3
4
  1
5

Is there a way?
0
Comment
Question by:originsone
3 Comments
 
LVL 25

Accepted Solution

by:
RonaldBiemans earned 500 total points
Comment Utility
yep, like this

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       TreeView1.Nodes(1).Nodes.Add("1")
        TreeView1.Nodes(1).Nodes.Add("2")
        TreeView1.Nodes(3).Nodes.Add("1")
end sub
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
Here are two different examples.  The first adds two children to the currently selected node.  The second add two children to the 5th node.

Public Class Form1
    Inherits System.Windows.Forms.Form

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim i As Integer
        For i = 1 To 10
            TreeView1.Nodes.Add("Node" & i)
        Next
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim curNode As TreeNode = TreeView1.SelectedNode ' get currently selected node
        If Not (curNode Is Nothing) Then
            Dim i As Integer

            For i = 1 To 2
                curNode.Nodes.Add("Child" & i)
            Next i
            curNode.Expand()
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim curnode As TreeNode = TreeView1.Nodes(4) ' get 5th node in root
        curnode.Nodes.Add("Child1")
        curnode.Nodes.Add("Child2")
    End Sub

End Class
0
 

Author Comment

by:originsone
Comment Utility
Simplicity. Thanks!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
This video discusses moving either the default database or any database to a new volume.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now