Creating Tree Views in Code using VB.net

By: Steven Holzner Emailed: 1728 times Printed: 2328 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

You can create tree views in code as well as at design time. The trick here is realizing that the hierarchical nature of tree views means that one node's Nodes collection can contain child nodes, which can itself contain child nodes, and so on, so you can refer to nodes using syntax like MyNode.Nodes(3). Nodes(5). To add a new node, you use the Nodes collection's Add method. Like other controls, you can use the BeginUpdate and EndUpdate methods to turn off updating of the tree view while you're updating it. Note that this example uses the Clear method to clear all nodes in the tree view before refilling it. (Note also that I'm using a few advanced programming techniques here, such as creating our own classes and storing objects in an ArrayList collection.) Here's the code, showing how to stock a tree view with nodes, and how to give those nodes child nodes, and so on, at run time:

Public Class Form1
    Inherits System.Windows.Forms.Form

    'Windows Form Designer generated code

    Private EmployeeArray As New ArrayList()

    Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click
        Dim intLoopIndex As Integer
        For intLoopIndex = 0 To 9
            EmployeeArray.Add(New Employee("Employee " & _
                intLoopIndex.ToString()))
        Next intLoopIndex

        Dim EmployeeObject As Employee
        For Each EmployeeObject In EmployeeArray
            For intLoopIndex = 0 To 3
                EmployeeObject.EmployeeAccounts.Add(New _
                    Account("Account " & intLoopIndex.ToString()))
            Next intLoopIndex
        Next EmployeeObject

        TreeView1.BeginUpdate()
        TreeView1.Nodes.Clear()

        Dim RootNode = New TreeNode("Employees")
        TreeView1.Nodes.Add(RootNode)

        For Each EmployeeObject In EmployeeArray
            TreeView1.Nodes(0).Nodes.Add(New _
                TreeNode(EmployeeObject.EmployeeName))

            Dim AccountObject As Account
            For Each AccountObject In EmployeeObject.EmployeeAccounts
                TreeView1.Nodes(0).Nodes(_
                    EmployeeArray.IndexOf(EmployeeObject)).Nodes.Add(_
                    New TreeNode(AccountObject.AccountID))
            Next AccountObject
        Next EmployeeObject

        TreeView1.EndUpdate()
    End Sub
End Class

Public Class Employee
    Private Index = 0
    Public EmployeeName As String
    Public EmployeeAccounts As New ArrayList()

    Public Sub New(ByVal Name As String)
        EmployeeName = Name
    End Sub
End Class

Public Class Account
    Public AccountID As String
    Public Sub New(ByVal ID As String)
        AccountID = ID
    End Sub
End Class

VB.net Home | All VB.net Tutorials | Latest VB.net Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Steven Holzner
Changes in Controls from VB6 to VB.net
Throwing a Custom Exception in VB.net
Throwing an Exception in VB.net
Using Finally in VB.net
Using Multiple Catch Statements in VB.net
Exception Filtering in the Catch Block in VB.net
Raising an Exception Intentionally in VB.net
Getting an Exception's Number and Description in VB.net
Using On Error GoTo 0 in VB.net
Using Resume Next and Resume Line in VB.net
Understanding Scope in VB.net
Properties in VB.net
Procedure Delegates in VB.net
Preserving a Variable's Values between Procedure Calls in VB.net
Specifying Optional Arguments with default values in Procedures in VB.net

More Tutorials in VB.net
Scope in VB.net
Unstructured Exception Handling in VB.net
Structured Exception Handling in VB.net
Creating Sub Procedures in VB.net
Creating Functions in VB.net
Passing a Variable Number of Arguments to Procedures in VB.net
Specifying Optional Arguments with default values in Procedures in VB.net
Preserving a Variable's Values between Procedure Calls in VB.net
Procedure Delegates in VB.net
Properties in VB.net
Understanding Scope in VB.net
Using Resume Next and Resume Line in VB.net
Using On Error GoTo 0 in VB.net
Getting an Exception's Number and Description in VB.net
Raising an Exception Intentionally in VB.net

More Latest News
Most Viewed Articles (in VB.net )
How to export from DataGridView to excel using VB.net
Your first VB.NET Crystal Reports - A step by step guide
Using Resume Next and Resume Line in VB.net
Debug and Release Versions in .NET
Preserving a Variable's Values between Procedure Calls in VB.net
The Option and Imports Statements in VB .NET
To read the entire worksheet in an Excel workbook through VB.net Code
Creating a Windows Service in VB.net
For Loop in VB.net
While Loop in VB.net
Create an Excel 2007 file in VB.NET 2005
How to Open and Edit Cells in an Excel 2007 file in VB.NET
Insert Pictures in Excel 2007 from VB.NET 2005
Read Data from Excel using OLEDB in VB.NET 2005
How to export from database to excel using VB.net
Most Emailed Articles (in VB.net)
How to export from DataGridView to excel using VB.net
Creating List Views in Code using VB.net
Converting between Data Types in VB.net
Handling Strings in VB.net
For Each…Next Loop in VB.net
String equals, Copy to & Copy to in VB.net
String endswith, concat in VB.net
How to Open and Edit Cells in an Excel 2007 file in VB.NET
Read Data from Excel using OLEDB in VB.NET 2005
Chat Server in VB.net
OleDbConnection class in VB.net
OleDbDataAdapter class in VB.net
Creating a Windows Service in VB.net
Using the ServiceProcessInstaller Class in VB.net
Using the ServiceInstaller Class in VB.net