vbnet
  1. vbnet-treeview-control

Working with Forms and Controls in VB.NET: TreeView Control

The TreeView control in VB.NET is used to display hierarchical data in a tree-like structure. It is commonly used to display file directories, organization charts, and family trees. This control can be customized with images, text, and other controls to make it more interactive.

Syntax

<asp:TreeView ID="TreeView1" runat="server">
</asp:TreeView>

Here, TreeView1 is the name of the control which can be used to manipulate the control in code.

Example

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' Populate the TreeView control with some nodes
        TreeView1.Nodes.Add("Node 1")
        TreeView1.Nodes.Add("Node 2")
        TreeView1.Nodes(1).Nodes.Add("SubNode 2.1")
        TreeView1.Nodes(1).Nodes.Add("SubNode 2.2")
        TreeView1.Nodes(1).Nodes(1).Nodes.Add("SubSubNode 2.2.1")
    End Sub

    Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
        ' Display the selected node text in a label
        Label1.Text = e.Node.Text
    End Sub

Output

TreeView Example Output

Explanation

In the above example, a TreeView control is added to the form and populated with some nodes in the form load event. The AfterSelect event is used to display the selected node text in a label control.

Use

The TreeView control can be used to display hierarchical data such as file directories, organization charts, and family trees. It can be customized with images, text, and other controls to make it more interactive.

Important Points

  • The TreeView control is used to display hierarchical data in a tree-like structure.
  • The control can be populated with nodes and subnodes to represent the hierarchical data.
  • The control can be customized with images, text, and other controls.
  • Events such as AfterSelect can be used to interact with the control.

Summary

In summary, the TreeView control in VB.NET is a powerful control for displaying hierarchical data in a tree-like structure. It can be customized with images, text, and other controls to make it more interactive. Events such as AfterSelect can be used to interact with the control and perform actions based on user input.

Published on: