VB.Net - TreeView Control

The TreeView control is used to display hierarchical representations of items similar to the ways the files and folders are displayed in the left pane of the Windows Explorer. Each node may contain one or more child nodes.
Let's click on a TreeView control from the Toolbox and place it on the form.
The following are some of the commonly used properties of the TreeView control −
BackColor
Gets or sets the background color for the control.
BackgroundImage
Gets or set the background image for the TreeView control.
BackgroundImageLayout
Gets or sets the layout of the background image for the TreeView control.
BorderStyle
Gets or sets the border style of the tree view control.
CheckBoxes
Gets or sets a value indicating whether check boxes are displayed next to the tree nodes in the tree view control.
DataBindings
Gets the data bindings for the control.
Font
Gets or sets the font of the text displayed by the control.
FontHeight
Gets or sets the height of the font of the control.
ForeColor
The current foreground color for this control, which is the color the control uses to draw its text.
ItemHeight
Gets or sets the height of each tree node in the tree view control.
Nodes
Gets the collection of tree nodes that are assigned to the tree view control.
PathSeparator
Gets or sets the delimiter string that the tree node path uses.
RightToLeftLayout
Gets or sets a value that indicates whether the TreeView should be laid out from right-to-left.
Scrollable
Gets or sets a value indicating whether the tree view control displays scroll bars when they are needed.
SelectedImageIndex
Gets or sets the image list index value of the image that is displayed when a tree node is selected.
SelectedImageKey
Gets or sets the key of the default image shown when a TreeNode is in a selected state.
SelectedNode
Gets or sets the tree node that is currently selected in the tree view control.
ShowLines
Gets or sets a value indicating whether lines are drawn between tree nodes in the tree view control.
ShowNodeToolTips
Gets or sets a value indicating ToolTips are shown when the mouse pointer hovers over a TreeNode.
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.
ShowRootLines
Gets or sets a value indicating whether lines are drawn between the tree nodes that are at the root of the tree view.
Sorted
Gets or sets a value indicating whether the tree nodes in the tree view are sorted.
StateImageList
Gets or sets the image list that is used to indicate the state of the TreeView and its nodes. 
Text
Gets or sets the text of the TreeView.
TopNode
Gets or sets the first fully-visible tree node in the tree view control.
TreeViewNodeSorter
Gets or sets the implementation of IComparer to perform a custom sort of the TreeView nodes.
VisibleCount
Gets the number of tree nodes that can be fully visible in the tree view control.
The following are some of the commonly used methods of the TreeView control −
CollapseAll
Collapses all the nodes including all child nodes in the tree view control.
ExpandAll
Expands all the nodes.
GetNodeAt
Gets the node at the specified location.
GetNodeCount
Gets the number of tree nodes.
Sort
Sorts all the items in the tree view control.
ToString
Returns a string containing the name of the control.
The following are some of the commonly used events of the TreeView control −
AfterCheck
Occurs after the tree node check box is checked.
AfterCollapse
Occurs after the tree node is collapsed.
AfterExpand
Occurs after the tree node is expanded.
AfterSelect
Occurs after the tree node is selected.
BeforeCheck
Occurs before the tree node check box is checked.
BeforeCollapse
Occurs before the tree node is collapsed.
BeforeExpand
Occurs before the tree node is expanded.
BeforeLabelEdit
Occurs before the tree node label text is edited.
BeforeSelect
Occurs before the tree node is selected.
ItemDrag
Occurs when the user begins dragging a node.
NodeMouseClick
Occurs when the user clicks a TreeNode with the mouse.
NodeMouseDoubleClick
Occurs when the user double-clicks a TreeNode with the mouse.
NodeMouseHover
Occurs when the mouse hovers over a TreeNode.
PaddingChanged
Occurs when the value of the Padding property changes.
Paint
Occurs when the TreeView is drawn.
RightToLeftLayoutChanged
Occurs when the value of the RightToLeftLayout property changes.
TextChanged
Occurs when the Text property changes.
The TreeNode class represents a node of a TreeView. Each node in a TreeView control is an object of the TreeNode class. To be able to use a TreeView control we need to have a look at some commonly used properties and methods of the TreeNode class.
The following are some of the commonly used properties of the TreeNode class −
BackColor
Gets or sets the background color of the tree node.
Checked
Gets or sets a value indicating whether the tree node is in a checked state.
ContextMenu
Gets the shortcut menu that is associated with this tree node.
ContextMenuStrip
Gets or sets the shortcut menu associated with this tree node.
FirstNode
Gets the first child tree node in the tree node collection.
FullPath
Gets the path from the root tree node to the current tree node.
Index
Gets the position of the tree node in the tree node collection.
IsEditing
Gets a value indicating whether the tree node is in an editable state.
IsExpanded
Gets a value indicating whether the tree node is in the expanded state.
IsSelected
Gets a value indicating whether the tree node is in the selected state.
IsVisible
Gets a value indicating whether the tree node is visible or partially visible.
LastNode
Gets the last child tree node.
Level
Gets the zero-based depth of the tree node in the TreeView control.
Name
Gets or sets the name of the tree node.
NextNode
Gets the next sibling tree node.
Nodes
Gets the collection of TreeNode objects assigned to the current tree node.
Parent
Gets the parent tree node of the current tree node.
PrevNode
Gets the previous sibling tree node.
PrevVisibleNode
Gets the previous visible tree node.
Tag
Gets or sets the object that contains data about the tree node.
Text
Gets or sets the text displayed in the label of the tree node.
ToolTipText
Gets or sets the text that appears when the mouse pointer hovers over a TreeNode.
TreeView
Gets the parent tree view that the tree node is assigned to.
The following are some of the commonly used methods of the TreeNode class −
Collapse
Collapses the tree node.
Expand
Expands the tree node.
ExpandAll
Expands all the child tree nodes.
GetNodeCount
Returns the number of child tree nodes.
Remove
Removes the current tree node from the tree view control.
Toggle
Toggles the tree node to either the expanded or collapsed state.
ToString
Returns a string that represents the current object.
In this example, let us create a tree view at runtime. Let's double click on the Form and put the follow code in the opened window.
Public Class Form1
   Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
      'create a new TreeView
      Dim TreeView1 As TreeView
      TreeView1 = New TreeView()
      TreeView1.Location = New Point(10, 10)
      TreeView1.Size = New Size(150, 150)
      
      Me.Controls.Add(TreeView1)
      TreeView1.Nodes.Clear()
      'Creating the root node
      Dim root = New TreeNode("Application")
      TreeView1.Nodes.Add(root)
      TreeView1.Nodes(0).Nodes.Add(New TreeNode("Project 1"))
      'Creating child nodes under the first child
      
      For loopindex As Integer = 1 To 4
         TreeView1.Nodes(0).Nodes(0).Nodes.Add(New  _
            TreeNode("Sub Project" & Str(loopindex)))
      Next loopindex
      ' creating child nodes under the root
      TreeView1.Nodes(0).Nodes.Add(New TreeNode("Project 6"))
      'creating child nodes under the created child node
      
      For loopindex As Integer = 1 To 3
         TreeView1.Nodes(0).Nodes(1).Nodes.Add(New  _
            TreeNode("Project File" & Str(loopindex)))
      Next loopindex
      ' Set the caption bar text of the form.  
      Me.Text = "tutorialspoint.com"
   End Sub
End Class
When the above code is executed and run using Start button available at the Microsoft Visual Studio tool bar, it will show the following window −
You can expand the nodes to see the child nodes −