Link to home
Start Free TrialLog in
Avatar of originsone
originsone

asked on

Adding children to a treeview after the main nodes are created

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?
ASKER CERTIFIED SOLUTION
Avatar of RonaldBiemans
RonaldBiemans

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mike Tomlinson
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
Avatar of originsone
originsone

ASKER

Simplicity. Thanks!