Example : treeview control at runtime in vb.net

The following example code bind the nodes to the existing treeview at run time in vb.net.

treeview control generated at runtime

Private Sub Form1_Load(ByVal sender As System.Object, _
                           ByVal e As System.EventArgs) Handles MyBase.Load
        Dim city As TreeNode
        Dim Country As TreeNode
        Country = New TreeNode
        Country.Text = "India"
        TreeView1.Nodes.Add(Country)
        city = New TreeNode
        city.Text = "New Delhi"
        Country.Nodes.Add(city)
        city = New TreeNode
        city.Text = "Kolkata"
        Country.Nodes.Add(city)
        city = New TreeNode
        city.Text = "Lucknow"
        Country.Nodes.Add(city)
        Country.Expand()
 
        Country = New TreeNode
        Country.Text = "USA"
        TreeView1.Nodes.Add(Country)
        city = New TreeNode
        city.Text = "NewYork"
        Country.Nodes.Add(city)
        city = New TreeNode
        city.Text = "Washington"
        Country.Nodes.Add(city)
        Country.Expand()
    End Sub