Solved

Adding children to a treeview after the main nodes are created

Posted on 2004-09-24
3
334 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
ID: 12144665
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
ID: 12144676
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
ID: 12144714
Simplicity. Thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
A short film showing how OnPage and Connectwise integration works.
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

929 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

12 Experts available now in Live!

Get 1:1 Help Now