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.
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 Properties
The following given below are some commonly used Properties of the TreeView Control in VB.net.# TreeView Control in VB.net Properties Description 1. BackColor Gets or sets the background color for the control. 2. BackgroundImage Gets or set the background image for the TreeView control. 3. BackgroundImageLayout Gets or sets the layout of the background image for the TreeView control. 4. BorderStyle Gets or sets the border style of the tree view control. 5. CheckBoxes Gets or sets a value indicating whether check boxes are displayed next to the tree nodes in the tree view control. 6. DataBindings Gets the data bindings for the control. 7. Font Gets or sets the font of the text displayed by the control. 8. FontHeight Gets or sets the height of the font of the control. 9. ForeColor The current foreground color for this control, which is the color the control uses to draw its text. 10. ItemHeight Gets or sets the height of each tree node in the tree view control. 11. Nodes Gets the collection of tree nodes that are assigned to the tree view control. 12. PathSeparator Gets or sets the delimiter string that the tree node path uses. 13. RightToLeftLayout Gets or sets a value that indicates whether the TreeView should be laid out from right-to-left. 14. Scrollable Gets or sets a value indicating whether the tree view control displays scroll bars when they are needed. 15. SelectedImageIndex Gets or sets the image list index value of the image that is displayed when a tree node is selected. 16. SelectedImageKey Gets or sets the key of the default image shown when a TreeNode is in a selected state. 17. SelectedNode Gets or sets the tree node that is currently selected in the tree view control. 18. ShowLines Gets or sets a value indicating whether lines are drawn between tree nodes in the tree view control. 19. ShowNodeToolTips Gets or sets a value indicating ToolTips are shown when the mouse pointer hovers over a TreeNode. 20. ShowPlusMinus Gets or sets a value indicating whether plus-sign (+) and minus-sign (-) buttons are displayed next to tree nodes that contain child tree nodes. 21. ShowRootLines Gets or sets a value indicating whether lines are drawn between the tree nodes that are at the root of the tree view. 22. Sorted Gets or sets a value indicating whether the tree nodes in the tree view are sorted. 23. StateImageList Gets or sets the image list that is used to indicate the state of the TreeView and its nodes. 24. Text Gets or sets the text of the TreeView. 25. TopNode Gets or sets the first fully-visible tree node in the tree view control. 26. TreeViewNodeSorter Gets or sets the implementation of IComparer to perform a custom sort of the TreeView nodes. 27. VisibleCount Gets the number of tree nodes that can be fully visible in the tree view control.
TreeView Control in VB.net Methods
The following given below are some commonly used Methods of the TreeView Control in VB.net.# Method Description 1. GetNodeAt A 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. GetNodeCount It is used to count the number of nodes that are available in the tree view control. 5. CollapseAll It is used to collapse all tree nodes, including all child nodes in the tree view control. 6. ToString ToString method is used to return the name of the string that is in the tree view control.
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 Events Description 1. AfterCheck Occurs after the tree node check box is checked. 2. AfterCollapse Occurs after the tree node is collapsed. 3. AfterExpand Occurs after the tree node is expanded. 4. AfterSelect Occurs after the tree node is selected. 5. BeforeCheck Occurs before the tree node check box is checked. 6. BeforeCollapse Occurs before the tree node is collapsed. 7. BeforeExpand Occurs before the tree node is expanded. 8. BeforeLabelEdit Occurs before the tree node label text is edited. 9. BeforeSelect Occurs before the tree node is selected. 10. ItemDrag Occurs when the user begins dragging a node. 11. NodeMouseClick Occurs when the user clicks a TreeNode with the mouse. 12. NodeMouseDoubleClick Occurs when the user double-clicks a TreeNode with the mouse. 13. NodeMouseHover Occurs when the mouse hovers over a TreeNode. 14. PaddingChanged Occurs when the value of the Padding property changes. 15. Paint Occurs when the TreeView is drawn. 16. RightToLeftLayoutChanged Occurs when the value of the RightToLeftLayout property changes. 17. TextChanged Occurs when the Text property changes.
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:
Click on the Add New button to insert a new node into the tree view control. It displays the following image on the monitor.
Click on the OK button to insert the ‘Programming Languages‘ node in the tree view control.
In the same way, we can add new nodes to the TreeView Control in VB.net, as shown below.
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.
PREVIOUS
NEXT








