Click here to Skip to main content
15,881,172 members
Articles / Programming Languages / Visual Basic

The Basic Operations on using the TreeView Control

Rate me:
Please Sign up or sign in to vote.
2.77/5 (9 votes)
30 Sep 20052 min read 81.3K   1.2K   17  
How to do basic operations (i.e. add, delete nodes) on the TreeView control in your VB.NET applications.
Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    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

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents AddParent As System.Windows.Forms.Button
    Friend WithEvents AddChild As System.Windows.Forms.Button
    Friend WithEvents DeleteNode As System.Windows.Forms.Button
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Text1 As System.Windows.Forms.TextBox
    Friend WithEvents TView As System.Windows.Forms.TreeView
    Friend WithEvents ImageList1 As System.Windows.Forms.ImageList
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
        Me.TView = New System.Windows.Forms.TreeView
        Me.AddParent = New System.Windows.Forms.Button
        Me.AddChild = New System.Windows.Forms.Button
        Me.DeleteNode = New System.Windows.Forms.Button
        Me.Text1 = New System.Windows.Forms.TextBox
        Me.Label1 = New System.Windows.Forms.Label
        Me.ImageList1 = New System.Windows.Forms.ImageList(Me.components)
        Me.SuspendLayout()
        '
        'TView
        '
        Me.TView.BackColor = System.Drawing.Color.IndianRed
        Me.TView.ImageIndex = -1
        Me.TView.Location = New System.Drawing.Point(24, 8)
        Me.TView.Name = "TView"
        Me.TView.SelectedImageIndex = -1
        Me.TView.Size = New System.Drawing.Size(248, 200)
        Me.TView.TabIndex = 0
        '
        'AddParent
        '
        Me.AddParent.BackColor = System.Drawing.Color.FromArgb(CType(192, Byte), CType(255, Byte), CType(192, Byte))
        Me.AddParent.Font = New System.Drawing.Font("Palatino Linotype", 9.75!, CType((System.Drawing.FontStyle.Bold Or System.Drawing.FontStyle.Italic), System.Drawing.FontStyle), System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.AddParent.Location = New System.Drawing.Point(8, 224)
        Me.AddParent.Name = "AddParent"
        Me.AddParent.Size = New System.Drawing.Size(96, 32)
        Me.AddParent.TabIndex = 1
        Me.AddParent.Text = "Add &Parent"
        '
        'AddChild
        '
        Me.AddChild.BackColor = System.Drawing.Color.FromArgb(CType(192, Byte), CType(255, Byte), CType(192, Byte))
        Me.AddChild.Font = New System.Drawing.Font("Palatino Linotype", 9.75!, CType((System.Drawing.FontStyle.Bold Or System.Drawing.FontStyle.Italic), System.Drawing.FontStyle), System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.AddChild.Location = New System.Drawing.Point(104, 224)
        Me.AddChild.Name = "AddChild"
        Me.AddChild.Size = New System.Drawing.Size(96, 32)
        Me.AddChild.TabIndex = 2
        Me.AddChild.Text = "Add &Child"
        '
        'DeleteNode
        '
        Me.DeleteNode.BackColor = System.Drawing.Color.FromArgb(CType(192, Byte), CType(255, Byte), CType(192, Byte))
        Me.DeleteNode.Font = New System.Drawing.Font("Palatino Linotype", 9.75!, CType((System.Drawing.FontStyle.Bold Or System.Drawing.FontStyle.Italic), System.Drawing.FontStyle), System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.DeleteNode.Location = New System.Drawing.Point(200, 224)
        Me.DeleteNode.Name = "DeleteNode"
        Me.DeleteNode.Size = New System.Drawing.Size(96, 32)
        Me.DeleteNode.TabIndex = 3
        Me.DeleteNode.Text = "&Delete"
        '
        'Text1
        '
        Me.Text1.BackColor = System.Drawing.Color.FromArgb(CType(255, Byte), CType(255, Byte), CType(192, Byte))
        Me.Text1.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Text1.Location = New System.Drawing.Point(72, 272)
        Me.Text1.Name = "Text1"
        Me.Text1.Size = New System.Drawing.Size(208, 20)
        Me.Text1.TabIndex = 4
        Me.Text1.Text = ""
        '
        'Label1
        '
        Me.Label1.BackColor = System.Drawing.Color.FromArgb(CType(192, Byte), CType(192, Byte), CType(255, Byte))
        Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label1.Location = New System.Drawing.Point(16, 272)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(48, 16)
        Me.Label1.TabIndex = 5
        Me.Label1.Text = "NODE"
        '
        'ImageList1
        '
        Me.ImageList1.ImageSize = New System.Drawing.Size(16, 16)
        Me.ImageList1.ImageStream = CType(resources.GetObject("ImageList1.ImageStream"), System.Windows.Forms.ImageListStreamer)
        Me.ImageList1.TransparentColor = System.Drawing.Color.Transparent
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.BackColor = System.Drawing.Color.FromArgb(CType(192, Byte), CType(192, Byte), CType(255, Byte))
        Me.ClientSize = New System.Drawing.Size(304, 317)
        Me.Controls.Add(Me.Label1)
        Me.Controls.Add(Me.Text1)
        Me.Controls.Add(Me.DeleteNode)
        Me.Controls.Add(Me.AddChild)
        Me.Controls.Add(Me.AddParent)
        Me.Controls.Add(Me.TView)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub AddParent_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddParent.Click
        TView.Nodes.Add(Text1.Text)
    End Sub

    Private Sub AddChild_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddChild.Click
        TView.SelectedNode.Nodes.Add(Text1.Text)
    End Sub

    Private Sub DeleteNode_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteNode.Click
        TView.SelectedNode.Remove()
    End Sub
End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Instructor / Trainer
Turkey Turkey
Asst. Prof. in Eastern Mediterranean University.

Comments and Discussions