A tree view is a graphical user
interface element that present a hierarchical view of information. Each item can
have a number of subitems. You can customize the tree, by choosing the text
font, icons, connector types, spacing and other properties.
Here I take a example to explain
the TreeView control in VB.NET in this example you will see how to create a
TreeView and how to create a pop-up menu on right-click of every node and child
node to delete the node.
Example of TreeView
Code to write in AssemblyInfo
class(AssemblyInfo.vb)
Imports System
Imports
System.Reflection
Imports
System.Runtime.InteropServices
<Assembly:
AssemblyTitle("")>
<Assembly:
AssemblyDescription("")>
<Assembly:
AssemblyCompany("")>
<Assembly:
AssemblyProduct("")>
<Assembly:
AssemblyCopyright("")>
<Assembly:
AssemblyTrademark("")>
<Assembly:
CLSCompliant(True)>
<Assembly: Guid("CE6847FE-1A5A-406C-B81F-736B3E7867B0")>
<Assembly:
AssemblyVersion("1.0.*")>
Code to write in Main
Form(Form1.vb)
Public
Class Form1
Inherits System.Windows.Forms.Form
#Region
" Windows code "
Public Sub
New()
MyBase.New()
InitializeComponent()
End Sub
Protected
Overloads Overrides
Sub Dispose(ByVal
disposing As Boolean)
If disposing
Then
If Not
(components Is
Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
Private components
As System.ComponentModel.IContainer
Friend WithEvents
Trees As System.Windows.Forms.ImageList
Friend
WithEvents Orgn As System.Windows.Forms.TreeView
Friend
WithEvents Buss As System.Windows.Forms.ContextMenu
Friend
WithEvents Group As
System.Windows.Forms.ContextMenu
Friend
WithEvents Names As
System.Windows.Forms.ContextMenu
Friend
WithEvents BussDelete As
System.Windows.Forms.MenuItem
Friend
WithEvents GroupDelete As
System.Windows.Forms.MenuItem
Friend
WithEvents NamesDelete As
System.Windows.Forms.MenuItem
<System.Diagnostics.DebuggerStepThrough()>
Private Sub
InitializeComponent()
Me.components =
New System.ComponentModel.Container()
Me.Orgn = New
System.Windows.Forms.TreeView()
Me.Trees =
New System.Windows.Forms.ImageList(Me.components)
Me.Buss = New
System.Windows.Forms.ContextMenu()
Me.BussDelete =
New System.Windows.Forms.MenuItem()
Me.Group =
New System.Windows.Forms.ContextMenu()
Me.GroupDelete =
New System.Windows.Forms.MenuItem()
Me.Names =
New System.Windows.Forms.ContextMenu()
Me.NamesDelete =
New System.Windows.Forms.MenuItem()
Me.SuspendLayout()
Me.Orgn.Dock = System.Windows.Forms.DockStyle.Fill
Me.Orgn.Location =
New System.Drawing.Point(0,
0)
Me.Orgn.Name =
"Orgn"
Me.Orgn.Size =
New System.Drawing.Size(260,
300)
Me.Orgn.TabIndex = 0
Me.Trees.ColorDepth =
System.Windows.Forms.ColorDepth.Depth8Bit
Me.Trees.ImageSize =
New System.Drawing.Size(14,
14)
Me.Trees.TransparentColor =
System.Drawing.Color.Transparent
Me.Buss.MenuItems.AddRange(New
System.Windows.Forms.MenuItem() {Me.BussDelete})
Me.BussDelete.Index = 0
Me.BussDelete.Text =
"Delete"
Me.Group.MenuItems.AddRange(New
System.Windows.Forms.MenuItem() {Me.GroupDelete})
Me.GroupDelete.Index = 0
Me.GroupDelete.Text =
"Delete"
Me.Names.MenuItems.AddRange(New
System.Windows.Forms.MenuItem() {Me.NamesDelete})
Me.NamesDelete.Index = 0
Me.NamesDelete.Text =
"Delete"
Me.AutoScaleBaseSize =
New System.Drawing.Size(5,
13)
Me.ClientSize =
New System.Drawing.Size(260,
300)
Me.Controls.Add(Me.Orgn)
Me.Name =
"Form1"
Me.Text =
"Form1"
Me.ResumeLayout(False)
End
Sub
#End
Region
Private
Sub Form1_Load(ByVal sender
As System.Object,
ByVal e As
System.EventArgs)
Handles MyBase.Load
Const Bussines
As Integer = 0
Const GroupsAll
As Integer = 2
Const AllNames
As Integer = 3
Dim BussinesV
As TreeNode
Dim group
As TreeNode
Dim PersonV
As TreeNode
BussinesV = AddTreeViewNode(Orgn.Nodes,
"Export", Bussines,
New BussinesVData("Export"))
group = AddTreeViewNode(BussinesV.Nodes,
"Manager", GroupsAll, New
GroupData("Manager"))
PersonV = AddTreeViewNode(group.Nodes,
"Manish", AllNames, New
PersonVData("Manish"))
PersonV.EnsureVisible()
group = AddTreeViewNode(BussinesV.Nodes,
"Worker", GroupsAll, New
GroupData("Worker"))
PersonV = AddTreeViewNode(group.Nodes, "Tewatia",
AllNames, New
PersonVData("Tewatia"))
PersonV.EnsureVisible()
End Sub
Private Function
AddTreeViewNode(ByVal parent_nodes
As
TreeNodeCollection, ByVal text
As String,
ByVal image_index As
Integer, Optional
ByVal tag_object As
Object = Nothing)
As TreeNode
Dim new_node
As TreeNode
= parent_nodes.Add(text)
new_node.ImageIndex = image_index
new_node.SelectedImageIndex = image_index
new_node.Tag = tag_object
Return new_node
End Function
Private
Sub BussDelete_Click(ByVal sender
As System.Object,
ByVal e As
System.EventArgs)
Handles BussDelete.Click
Orgn.SelectedNode.Remove()
End Sub
Private
Sub GroupDelete_Click(ByVal sender
As System.Object,
ByVal e As
System.EventArgs)
Handles GroupDelete.Click
Orgn.SelectedNode.Remove()
End Sub
Private
Sub NamesDelete_Click(ByVal sender
As System.Object,
ByVal e As
System.EventArgs)
Handles NamesDelete.Click
Orgn.SelectedNode.Remove()
End Sub
Private
Sub Orgn_MouseDown(ByVal sender
As Object,
ByVal e As
System.Windows.Forms.MouseEventArgs)
Handles Orgn.MouseDown
If e.Button = MouseButtons.Right
Then
Dim node_here As
TreeNode = Orgn.GetNodeAt(e.X, e.Y)
Orgn.SelectedNode = node_here
If node_here
Is Nothing
Then Exit Sub
If
TypeOf node_here.Tag Is
BussinesVData Then
Buss.Show(Orgn, New
Point(e.X, e.Y))
ElseIf
TypeOf node_here.Tag Is
GroupData Then
Group.Show(Orgn, New
Point(e.X, e.Y))
ElseIf
TypeOf node_here.Tag Is
PersonVData Then
Names.Show(Orgn, New
Point(e.X, e.Y))
End If
End If
End
Sub
End
Class
Public
Class
BussinesVData
Public Name
As String
Public Sub
New(ByVal
new_name As String)
Name = new_name
End
Sub
End
Class
Public
Class GroupData
Public Name
As String
Public Projects
As Collection
Public Sub
New(ByVal
new_name As String,
ByVal ParamArray
project_names() As
String)
Name = new_name
Projects = New
Collection
For i
As Integer = 0
To project_names.GetUpperBound(0)
Projects.Add(project_names(i))
Next i
End
Sub
End
Class
Public
Class PersonVData
Public Name
As String
Public Projects
As Collection
Public Sub
New(ByVal
new_name As String,
ByVal ParamArray
project_names() As
String)
Name = new_name
Projects = New
Collection
For i
As Integer = 0
To project_names.GetUpperBound(0)
Projects.Add(project_names(i))
Next i
End
Sub
End
Class
Output of the above code

Conclusion
I hope this article helps you
understand the TreeView control in VB.NET.