Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | Videos | Photos | Blogs | Beginners | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Mindcracker MVP Summit 2012
Search :       Advanced Search »
Home » Visual Basic Language » Working with TreeView in VB.NET

Working with TreeView in VB.NET

In this article you will how to create and work with an tree view in VB.NET.

Author Rank :
Page Views : 10227
Downloads : 244
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
TreeView.zip
 
 
Discover the top 5 tips for understanding .NET Interop
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


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

tree1.gif

Conclusion

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

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Manish Tewatia
Manish is Very much interested in Microsoft & LifeStyle Accessory Designand working with Microsoft technologies. His expert areas are ASP.NET, ADO.NET, C# .NET, WPF, WCF, Windows Phone 7, Android, SQL Server, HTML , XAML, etc…
He is doing MCA and he love to drive fast bikes.
inspired from Mr. Mahesh Chand.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the top 5 tips for understanding .NET
Ricky Leeks presents the top 5 tips for understanding .NET Interoperability. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Team Foundation Server Hosting
Become a Sponsor
 Comments
Mindcracker MVP Summit 2012
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.