Option Strict On 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 Friend WithEvents TreeView1 As System.Windows.Forms.TreeView Friend WithEvents bttnCategories As System.Windows.Forms.Button Friend WithEvents bttnShapes As System.Windows.Forms.Button Friend WithEvents bttnPopulate As System.Windows.Forms.Button Friend WithEvents Button5 As System.Windows.Forms.Button Friend WithEvents bttnPopulateSorted As System.Windows.Forms.Button Friend WithEvents brrnClear As System.Windows.Forms.Button Friend WithEvents bttnColors As System.Windows.Forms.Button Friend WithEvents bttnMoveTree As System.Windows.Forms.Button 'Required by the Windows Form Designer Private components As System.ComponentModel.Container '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. Private Sub InitializeComponent() Me.TreeView1 = New System.Windows.Forms.TreeView() Me.bttnColors = New System.Windows.Forms.Button() Me.Button5 = New System.Windows.Forms.Button() Me.bttnPopulateSorted = New System.Windows.Forms.Button() Me.bttnShapes = New System.Windows.Forms.Button() Me.bttnPopulate = New System.Windows.Forms.Button() Me.brrnClear = New System.Windows.Forms.Button() Me.bttnCategories = New System.Windows.Forms.Button() Me.bttnMoveTree = New System.Windows.Forms.Button() Me.SuspendLayout() ' 'TreeView1 ' Me.TreeView1.Font = New System.Drawing.Font("Comic Sans MS", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte)) Me.TreeView1.ImageIndex = -1 Me.TreeView1.Location = New System.Drawing.Point(8, 8) Me.TreeView1.Name = "TreeView1" Me.TreeView1.SelectedImageIndex = -1 Me.TreeView1.Size = New System.Drawing.Size(240, 304) Me.TreeView1.TabIndex = 0 ' 'bttnColors ' Me.bttnColors.Enabled = False Me.bttnColors.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte)) Me.bttnColors.Location = New System.Drawing.Point(256, 46) Me.bttnColors.Name = "bttnColors" Me.bttnColors.Size = New System.Drawing.Size(136, 32) Me.bttnColors.TabIndex = 2 Me.bttnColors.Text = "Add Colors" ' 'Button5 ' Me.Button5.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte)) Me.Button5.Location = New System.Drawing.Point(256, 160) Me.Button5.Name = "Button5" Me.Button5.Size = New System.Drawing.Size(136, 32) Me.Button5.TabIndex = 5 Me.Button5.Text = "Sort Items" ' 'bttnPopulateSorted ' Me.bttnPopulateSorted.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte)) Me.bttnPopulateSorted.Location = New System.Drawing.Point(256, 198) Me.bttnPopulateSorted.Name = "bttnPopulateSorted" Me.bttnPopulateSorted.Size = New System.Drawing.Size(136, 32) Me.bttnPopulateSorted.TabIndex = 6 Me.bttnPopulateSorted.Text = "Populate Sorted" ' 'bttnShapes ' Me.bttnShapes.Enabled = False Me.bttnShapes.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte)) Me.bttnShapes.Location = New System.Drawing.Point(256, 84) Me.bttnShapes.Name = "bttnShapes" Me.bttnShapes.Size = New System.Drawing.Size(136, 32) Me.bttnShapes.TabIndex = 3 Me.bttnShapes.Text = "Add Shapes" ' 'bttnPopulate ' Me.bttnPopulate.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte)) Me.bttnPopulate.Location = New System.Drawing.Point(256, 122) Me.bttnPopulate.Name = "bttnPopulate" Me.bttnPopulate.Size = New System.Drawing.Size(136, 32) Me.bttnPopulate.TabIndex = 4 Me.bttnPopulate.Text = "Populate" ' 'brrnClear ' Me.brrnClear.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte)) Me.brrnClear.Location = New System.Drawing.Point(256, 236) Me.brrnClear.Name = "brrnClear" Me.brrnClear.Size = New System.Drawing.Size(136, 32) Me.brrnClear.TabIndex = 6 Me.brrnClear.Text = "Clear Control" ' 'bttnCategories ' Me.bttnCategories.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte)) Me.bttnCategories.Location = New System.Drawing.Point(256, 8) Me.bttnCategories.Name = "bttnCategories" Me.bttnCategories.Size = New System.Drawing.Size(136, 32) Me.bttnCategories.TabIndex = 1 Me.bttnCategories.Text = "Add Categories" ' 'bttnMoveTree ' Me.bttnMoveTree.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte)) Me.bttnMoveTree.Location = New System.Drawing.Point(256, 274) Me.bttnMoveTree.Name = "bttnMoveTree" Me.bttnMoveTree.Size = New System.Drawing.Size(136, 32) Me.bttnMoveTree.TabIndex = 7 Me.bttnMoveTree.Text = "Move Tree" ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(398, 319) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.bttnMoveTree, Me.brrnClear, Me.bttnPopulateSorted, Me.Button5, Me.bttnPopulate, Me.bttnShapes, Me.bttnColors, Me.bttnCategories, Me.TreeView1}) Me.Name = "Form1" Me.Text = "TreeView Demo" Me.ResumeLayout(False) End Sub #End Region Private Sub bttnCategories_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnCategories.Click TreeView1.Nodes.Add("Shapes") TreeView1.Nodes.Add("Solids") TreeView1.Nodes.Add("Colors") EnableButtons() End Sub Private Sub bttnColors_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnColors.Click Dim cnode As TreeNode cnode = TreeView1.Nodes(2) cnode.Nodes.Add("Pink") cnode.Nodes.Add("Maroon") cnode.Nodes.Add("Teal") cnode.Expand() End Sub Private Sub bttnShapes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnShapes.Click Dim cnode As TreeNode cnode = TreeView1.Nodes(0) cnode.Nodes.Add("Square") cnode.Nodes.Add("Triangle") cnode.Nodes.Add("Circle") End Sub Private Sub bttnPopulate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnPopulate.Click Dim cnode As TreeNode TreeView1.Nodes.Add("Shapes") TreeView1.Nodes.Add("Solids") TreeView1.Nodes.Add("Colors") cnode = TreeView1.Nodes(0) cnode = TreeView1.Nodes(0) cnode.Nodes.Add("Square") cnode.Nodes.Add("Triangle") cnode.Nodes.Add("Circle") cnode = TreeView1.Nodes(2) cnode.Nodes.Add("Pink") cnode.Nodes.Add("Maroon") cnode.Nodes.Add("Teal") cnode.Expand() End Sub Private Sub bttnPopulateSorted_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnPopulateSorted.Click TreeView1.Sorted = True bttnPopulate_Click(sender, e) End Sub Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click TreeView1.Sorted = True End Sub Private Sub brrnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles brrnClear.Click TreeView1.Nodes.Clear() DisableButtons() End Sub Sub EnableButtons() bttnColors.Enabled = True bttnShapes.Enabled = True End Sub Sub DisableButtons() bttnColors.Enabled = False bttnShapes.Enabled = False End Sub ' Notice that you can't move the entire tree under the same root twice. ' If you click this button for a second time, a run-time error will occur Private Sub bttnMoveTree_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnMoveTree.Click Dim colorNode, shapeNode, solidNode As TreeNode colorNode = TreeView1.Nodes(0) shapeNode = TreeView1.Nodes(1) solidNode = TreeView1.Nodes(2) TreeView1.Nodes.Clear() TreeView1.Nodes.Add("Items") TreeView1.Nodes(0).Nodes.Add(colorNode) TreeView1.Nodes(0).Nodes.Add(shapeNode) TreeView1.Nodes(0).Nodes.Add(solidNode) End Sub End Class