VB.NET Simple TreeView - Add TreeNodes

July 17, 2017 Oclemy VB.NET TreeView 1 minute, 1 second

This is a simple treeview example in vb.net.We see how to add treenodes at three levels.First Node,which is our root node,in this case country.Second level node,in this case Country and finally third-leve node in this case cities,where we add multiple cities.

Public Class Form1

    Dim node As TreeNode

    'ADDING  ROOT NODE
    Private Sub addRootBtn_Click(sender As Object, e As EventArgs) Handles addRootBtn.Click
        TreeView1.Nodes.Clear()

        Dim text As String
        text = TextBox1.Text

        If text IsNot "" Then
            node = TreeView1.Nodes.Add(text)
        End If
        'CLEAR TEXTBOX
        TextBox1.Text = ""
    End Sub

    'ADDING  FIRST LEVEL NODE
    Private Sub AddFirstBtn_Click(sender As Object, e As EventArgs) Handles AddFirstBtn.Click
        TreeView1.Nodes(0).Nodes.Clear()

        Dim text As String
        text = TextBox2.Text

        If text IsNot "" Then
            node = TreeView1.Nodes(0).Nodes.Add(text)
        End If

        'CLEAR TEXTBOX
        TextBox2.Text = ""

    End Sub

    Private Sub addSecondBtn_Click(sender As Object, e As EventArgs) Handles addSecondBtn.Click
        Dim text As String
        text = TextBox3.Text

        If text IsNot "" Then
            node = TreeView1.Nodes(0).Nodes(0).Nodes.Add(text)
        End If

        'CLEAR TEXTBOX
        TextBox3.Text = ""
    End Sub

    'CLEAR
    Private Sub clearBtn_Click(sender As Object, e As EventArgs) Handles clearBtn.Click
        TreeView1.Nodes.Clear()

    End Sub
End Class

Comments