Visual Basic .NET - TreeView Control

The VB.NET TreeView control is used to display a hierarchical list of items in a tree structure. It provides a user-friendly way to display and navigate through a large amount of information.

Properties

  • Nodes: This property gets or sets a collection of TreeNode objects representing the root nodes of the tree.
  • SelectedNode: This property gets or sets the currently selected tree node.
  • HideSelection: This property gets or sets a value indicating whether the selected tree node remains highlighted even when the TreeView control loses focus.
  • FullRowSelect: This property gets or sets a value indicating whether the tree node label and any images associated with the node are included in the selection.
  • CheckBoxes: This property gets or sets a value indicating whether check boxes are displayed next to the tree nodes.

Methods

  • BeginUpdate(): This method begins the process of updating the TreeView control and prevents the control from repainting until the EndUpdate method is called.
  • EndUpdate(): This method ends the process of updating the TreeView control and causes it to be repainted.
  • CollapseAll(): This method collapses all the tree nodes.
  • ExpandAll(): This method expands all the tree nodes.
  • FindNodeWithTag(): This method searches for a TreeNode object whose Tag property matches the specified value.

Events

  • AfterSelect: This event occurs after a tree node is selected.
  • BeforeSelect: This event occurs before a tree node is selected.
  • AfterCheck: This event occurs after the check box for a tree node is checked or unchecked.
  • BeforeCheck: This event occurs before the check box for a tree node is checked or unchecked.
  • NodeMouseClick: This event occurs when the user clicks a tree node with the mouse.
' Create a new TreeNode and add it to the TreeView control
Dim node1 As New TreeNode("Node 1")
TreeView1.Nodes.Add(node1)
' Add child nodes to the first node
node1.Nodes.Add("Child Node 1")
node1.Nodes.Add("Child Node 2")
' Create a new root node and add it to the TreeView control
Dim node2 As New TreeNode("Node 2")
TreeView1.Nodes.Add(node2)
' Add child nodes to the second node
node2.Nodes.Add("Child Node 3")
node2.Nodes.Add("Child Node 4")

This code creates two root nodes with child nodes under each. The resulting TreeView control will display a hierarchical list of four nodes.

Let's say you want to create a file explorer type of program that displays the directory structure of a computer using the TreeView control. Here's how you can achieve this:

Drag and drop a TreeView control onto the form.

In the Form_Load event, add the following code:

Private Sub Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    ' Set the root directory to display in the TreeView control
    Dim rootNode As TreeNode = New TreeNode("Computer")
    rootNode.Tag = "root"
    TreeView1.Nodes.Add(rootNode)
    PopulateTreeView("C:\", rootNode)
End Sub

In the above code, we first create a root node for the TreeView control with the text "Computer". We also set the Tag property to "root" so we can identify it later.

We then add this rootNode to the Nodes collection of the TreeView control.

Finally, we call the PopulateTreeView function with the root directory path "C:" and the rootNode as arguments. This function will recursively populate the TreeView control with all the directories and subdirectories under the specified root directory.

Add the following function to the code:

Private Sub PopulateTreeView(ByVal dir As String, ByVal parentNode As TreeNode)
    Dim folder As String = String.Empty
    Try
        Dim folders() As String = IO.Directory.GetDirectories(dir)
        If folders.Length <> 0 Then
            Dim childNode As TreeNode = Nothing
            For Each folder In folders
                childNode = New TreeNode(IO.Path.GetFileName(folder))
                childNode.Tag = folder
                parentNode.Nodes.Add(childNode)
                PopulateTreeView(folder, childNode)
            Next
        End If
    Catch ex As UnauthorizedAccessException
        parentNode.Nodes.Add(folder & ": Access Denied")
    End Try
End Sub

This function takes a directory path and a parent node as arguments. It gets all the subdirectories under the specified directory using the IO.Directory.GetDirectories method.

If there are any subdirectories, it creates a child node for each subdirectory with the text as the directory name and sets the Tag property to the directory path.

It then recursively calls itself with the child node as the parent node to populate the child node with all its subdirectories.

If there's an exception thrown when trying to access a subdirectory, it adds a child node with the text "Access Denied".

Run the program and you should see a TreeView control displaying the directory structure of the computer starting from the root directory "C:".

You can add additional functionality to this program such as displaying files in the selected directory or opening a selected file by handling the TreeView control's events.