Create TreeView control and bind with nodes at runtime in vb.net

In this article we will discuss about how to create treeview at run time in vb.net. The example also shows that how can we add nodes to treeview control at run time in vb.net.

TreeView control at run time

The following illustration creates a tree-view control with a root node and its sub node.

    Private Sub CreateTreeViewAtRuntime()
        Dim TreeView1 As New TreeView
        TreeView1.Location = New System.Drawing.Point(32, 23)
        TreeView1.Name = "TreeView1"
        TreeView1.Size = New System.Drawing.Size(202, 198)
        Me.Controls.Add(TreeView1)
        'Now we add a node to this treeview so first we create a treenode see this:
        Dim Node1 As New TreeNode
        Node1.Text = "India"
        Node1.Tag = "Country"
        'Add above tree node to treeview
        TreeView1.Nodes.Add(Node1)
 
        'Now we create another sub node 
        Dim Node_of_Node1 As New TreeNode
        Node_of_Node1.Text = "New Delhi"
        Node_of_Node1.Tag = "City"
        'Add this node to Node1 
        Node1.Nodes.Add(Node_of_Node1)
    End Sub