TreeView Control in VB.net – Properties, Methods and Events

What is TreeView Control in VB.net?

The TreeView Control in VB.net is used to show the same data in a tree structure that shows how the data is related to each other.

In the TreeView in VB.net, the root node is at the top, and it may have one or more child nodes. Also, you can shrink or grow the root node by clicking on the plus sign (+) button. It is also helpful to give the full path from the root node to the child node.

See also all the Basic Control Using VB.net with Source Code….

In order for you to test your VB.net Code provided in this lesson, you must test the code in your code editor. But if you wish to run this code online, we also have an Online Compiler in VB.net for you to test your VB.net Code for free.

We have learned how to use the DateTimePicker Control in VB.net in the previous lesson. In this lesson we shall learn How To Write a Program for TreeView in VB.net.

Let’s create a TreeView in VB.net by dragging a TreeView Control from the Toolbox and dropping it on the form.

TreeView Control in VB.net Tutorial
TreeView Control in VB.net Tutorial

TreeView Control in VB.net Properties

The following given below are some commonly used Properties of the TreeView Control in VB.net.

#TreeView Control in VB.net PropertiesDescription
1.BackColorGets or sets the background color for the control.
2.BackgroundImageGets or set the background image for the TreeView control.
3.BackgroundImageLayoutGets or sets the layout of the background image for the TreeView control.
4.BorderStyleGets or sets the border style of the tree view control.
5.CheckBoxesGets or sets a value indicating whether check boxes are displayed next to the tree nodes in the tree view control.
6.DataBindingsGets the data bindings for the control.
7.FontGets or sets the font of the text displayed by the control.
8.FontHeightGets or sets the height of the font of the control.
9.ForeColorThe current foreground color for this control, which is the color the control uses to draw its text.
10.ItemHeightGets or sets the height of each tree node in the tree view control.
11.NodesGets the collection of tree nodes that are assigned to the tree view control.
12.PathSeparatorGets or sets the delimiter string that the tree node path uses.
13.RightToLeftLayoutGets or sets a value that indicates whether the TreeView should be laid out from right-to-left.
14.ScrollableGets or sets a value indicating whether the tree view control displays scroll bars when they are needed.
15.SelectedImageIndexGets or sets the image list index value of the image that is displayed when a tree node is selected.
16.SelectedImageKeyGets or sets the key of the default image shown when a TreeNode is in a selected state.
17.SelectedNodeGets or sets the tree node that is currently selected in the tree view control.
18.ShowLinesGets or sets a value indicating whether lines are drawn between tree nodes in the tree view control.
19.ShowNodeToolTipsGets or sets a value indicating ToolTips are shown when the mouse pointer hovers over a TreeNode.
20.ShowPlusMinusGets or sets a value indicating whether plus-sign (+) and minus-sign (-) buttons are displayed next to tree nodes that contain child tree nodes.
21.ShowRootLinesGets or sets a value indicating whether lines are drawn between the tree nodes that are at the root of the tree view.
22.SortedGets or sets a value indicating whether the tree nodes in the tree view are sorted.
23.StateImageListGets or sets the image list that is used to indicate the state of the TreeView and its nodes.
24.TextGets or sets the text of the TreeView.
25.TopNodeGets or sets the first fully-visible tree node in the tree view control.
26.TreeViewNodeSorterGets or sets the implementation of IComparer to perform a custom sort of the TreeView nodes.
27.VisibleCountGets the number of tree nodes that can be fully visible in the tree view control.
Properties and Description of TreeView Control in VB.net

TreeView Control in VB.net Methods

The following given below are some commonly used Methods of the TreeView Control in VB.net.

#MethodDescription
1.GetNodeAtA GetNodeAt() method is used to get a node at the specified location of the tree view control.
2.Sort()A Sort method is used to sort the tree nodes that are available in the tree view control.
3.ExpandAll()As the name suggests, an ExpandAll method is used to expand all the tree nodes.
4.GetNodeCountIt is used to count the number of nodes that are available in the tree view control.
5.CollapseAllIt is used to collapse all tree nodes, including all child nodes in the tree view control.
6.ToStringToString method is used to return the name of the string that is in the tree view control.
Methods and Descriptions of TreeView Control in VB.net

TreeView Control in VB.net Events

The following given below are some commonly used Events of the TreeView Control in VB.net.

#TreeView Control in VB.net EventsDescription
1.AfterCheckOccurs after the tree node check box is checked.
2.AfterCollapseOccurs after the tree node is collapsed.
3.AfterExpandOccurs after the tree node is expanded.
4.AfterSelectOccurs after the tree node is selected.
5.BeforeCheckOccurs before the tree node check box is checked.
6.BeforeCollapseOccurs before the tree node is collapsed.
7.BeforeExpandOccurs before the tree node is expanded.
8.BeforeLabelEditOccurs before the tree node label text is edited.
9.BeforeSelectOccurs before the tree node is selected.
10.ItemDragOccurs when the user begins dragging a node.
11.NodeMouseClickOccurs when the user clicks a TreeNode with the mouse.
12.NodeMouseDoubleClickOccurs when the user double-clicks a TreeNode with the mouse.
13.NodeMouseHoverOccurs when the mouse hovers over a TreeNode.
14.PaddingChangedOccurs when the value of the Padding property changes.
15.PaintOccurs when the TreeView is drawn.
16.RightToLeftLayoutChangedOccurs when the value of the RightToLeftLayout property changes.
17.TextChangedOccurs when the Text property changes.
Events and Descriptions of TreeView Control in VB.net

Let’s create a program to insert a node in the TreeView Control of the VB.NET Form.

Public Class Form1
    Private Sub Button1_Click_4(sender As Object, e As EventArgs) Handles Button1.Click
        Dim nd As String
        nd = InputBox("Enter Node name")  ' takes input from the user  
        If TreeView1.SelectedNode Is Nothing Then  ' insert a new node.  
            TreeView1.Nodes.Add(nd, nd)
        Else
            TreeView1.SelectedNode.Nodes.Add(nd, nd)   ' insert into the sub node or child node  
        End If
    End Sub
End Class

Program Output:

TreeView Control in VB.net Output
TreeView Control in VB.net Output

Click on the Add New button to insert a new node into the tree view control. It displays the following image on the monitor.

TreeView Control in VB.net Add New Node
TreeView Control in VB.net Add New Node

Click on the OK button to insert the ‘Programming Languages‘ node in the tree view control.

TreeView Control in VB.net Insert Node
TreeView Control in VB.net Insert Node

In the same way, we can add new nodes to the TreeView Control in VB.net, as shown below.

TreeView Control in VB.net Display all Nodes
TreeView Control in VB.net Display all Nodes

Summary

In this article, we talked about how to make and add multiple Node in a Windows Forms TreeView Control in VB.net using Microsoft Visual Studio both at design-time and at run-time. Then we learned how to use the different Properties, Methods and Events.


Leave a Comment