Treeview Control in VB.Net

The TreeView control in VB.Net serves as a valuable tool for displaying hierarchical and tree-like information, such as a directory hierarchy or organizational structure. It provides a structured and visually appealing representation of data that follows a parent-child relationship.

In a TreeView, the top level consists of root nodes. These root nodes can be expanded or collapsed to reveal or hide their child nodes, depending on whether the nodes have any child nodes attached to them. The expansion and collapsing of nodes can be accomplished by clicking on the plus (+) button next to a TreeNode, if one is displayed, or programmatically using the TreeNode.Expand method.

Expanding a parent node in a TreeView makes its child nodes visible, enabling users to explore the hierarchical structure. This hierarchical display allows for the organization and navigation of complex data sets in a user-friendly manner.

vb.net-treeview.jpg

The user can expand the TreeNode by clicking the plus sign (+) button, if one is displayed next to the TreeNode, or you can expand the TreeNode by calling the TreeNode.Expand method. When a parent node is expanded, its child nodes are visible. You can also navigate through tree views with various properties: FirstNode, LastNode, NextNode, PrevNode, NextVisibleNode, PrevVisibleNode.

TreeView control Properties

In addition to expanding and collapsing nodes, the TreeView control provides various properties that facilitate navigation within the tree structure. These properties include:

  1. FirstNode: Represents the first node in the TreeView.
  2. LastNode: Represents the last node in the TreeView.
  3. NextNode: Retrieves the next sibling node in the TreeView.
  4. PrevNode: Retrieves the previous sibling node in the TreeView.
  5. NextVisibleNode: Retrieves the next visible node in the TreeView, considering the expanded and collapsed state of nodes.
  6. PrevVisibleNode: Retrieves the previous visible node in the TreeView, considering the expanded and collapsed state of nodes.

The fullpath method of treeview control provides the path from root node to the selected node.

TreeView1.SelectedNode.FullPath

Tree nodes can optionally display check boxes. To display the check boxes, set the CheckBoxes property of the TreeView to true.

TreeView1.CheckBoxes = True

The following Vb.Net program shows a simple demonstration of treeview control.

Full Source VB.NET
Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim tNode As TreeNode tNode = TreeView1.Nodes.Add("Websites") TreeView1.Nodes(0).Nodes.Add("Net-informations.com") TreeView1.Nodes(0).Nodes(0).Nodes.Add("CLR") TreeView1.Nodes(0).Nodes.Add("https://net-informations.com/vb/default.htm") TreeView1.Nodes(0).Nodes(1).Nodes.Add("String Tutorial") TreeView1.Nodes(0).Nodes(1).Nodes.Add("Excel Tutorial") TreeView1.Nodes(0).Nodes.Add("Csharp.net-informations.com") TreeView1.Nodes(0).Nodes(2).Nodes.Add("ADO.NET") TreeView1.Nodes(0).Nodes(2).Nodes(0).Nodes.Add("Dataset") End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click MsgBox(TreeView1.SelectedNode.FullPath) End Sub End Class

Conclusion

The TreeView control in VB.Net provides an intuitive and organized way to display and interact with hierarchical data. Its expandable and collapsible nodes, along with navigation properties, facilitate efficient exploration and manipulation of tree-like information within your application.