Build file tree recursively : TreeView « GUI « VB.Net Tutorial






Build file tree recursively
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms

public class BuildFileTree
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class
Public Class Form1
    Private Sub ActTraverse_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles ActTraverse.Click
        If (My.Computer.FileSystem.DirectoryExists(StartPath.Text) = False) Then
            Exit Sub
        End If

        PathTree.Nodes.Clear()

        BuildDirectoryTree(Nothing, StartPath.Text)
    End Sub

    Private Sub BuildDirectoryTree(ByVal fromNode As TreeNode,ByVal basePath As String)
        Dim newDirectory As TreeNode
        Dim justTheSubdirectory As String

        For Each oneDirectory As String In My.Computer.FileSystem.GetDirectories(basePath)
            justTheSubdirectory = My.Computer.FileSystem.GetName(oneDirectory)

            If (fromNode Is Nothing) Then
                newDirectory = PathTree.Nodes.Add( justTheSubdirectory)
            Else
                newDirectory = fromNode.Nodes.Add(justTheSubdirectory)
            End If

            BuildDirectoryTree(newDirectory, My.Computer.FileSystem.CombinePath(basePath, justTheSubdirectory))
        Next oneDirectory
    End Sub
End Class


<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        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.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.ActTraverse = New System.Windows.Forms.Button
        Me.StartPath = New System.Windows.Forms.TextBox
        Me.PathTree = New System.Windows.Forms.TreeView
        Me.Label1 = New System.Windows.Forms.Label
        Me.Label2 = New System.Windows.Forms.Label
        Me.SuspendLayout()
        '
        'ActTraverse
        '
        Me.ActTraverse.Location = New System.Drawing.Point(326, 32)
        Me.ActTraverse.Name = "ActTraverse"
        Me.ActTraverse.Size = New System.Drawing.Size(75, 23)
        Me.ActTraverse.TabIndex = 2
        Me.ActTraverse.Text = "Traverse"
        Me.ActTraverse.UseVisualStyleBackColor = True
        '
        'StartPath
        '
        Me.StartPath.Location = New System.Drawing.Point(72, 8)
        Me.StartPath.Name = "StartPath"
        Me.StartPath.Size = New System.Drawing.Size(328, 20)
        Me.StartPath.TabIndex = 1
        '
        'PathTree
        '
        Me.PathTree.Location = New System.Drawing.Point(72, 64)
        Me.PathTree.Name = "PathTree"
        Me.PathTree.Size = New System.Drawing.Size(329, 184)
        Me.PathTree.TabIndex = 4
        '
        'Label1
        '
        Me.Label1.AutoSize = True
        Me.Label1.Location = New System.Drawing.Point(8, 10)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(57, 13)
        Me.Label1.TabIndex = 0
        Me.Label1.Text = "Start Path:"
        '
        'Label2
        '
        Me.Label2.AutoSize = True
        Me.Label2.Location = New System.Drawing.Point(8, 66)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(57, 13)
        Me.Label2.TabIndex = 3
        Me.Label2.Text = "Path Tree:"
        '
        'Form1
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(411, 260)
        Me.Controls.Add(Me.Label2)
        Me.Controls.Add(Me.Label1)
        Me.Controls.Add(Me.PathTree)
        Me.Controls.Add(Me.StartPath)
        Me.Controls.Add(Me.ActTraverse)
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
        Me.MaximizeBox = False
        Me.Name = "Form1"
        Me.Text = "Directory Traversal"
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub
    Friend WithEvents ActTraverse As System.Windows.Forms.Button
    Friend WithEvents StartPath As System.Windows.Forms.TextBox
    Friend WithEvents PathTree As System.Windows.Forms.TreeView
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label

End Class








14.42.TreeView
14.42.1.Creating Tree Views in CodeCreating Tree Views in Code
14.42.2.Add and remove tree nodesAdd and remove tree nodes
14.42.3.Build file tree recursivelyBuild file tree recursively
14.42.4.Using a TreeView to display the directory structureUsing a TreeView to display the directory structure
14.42.5.Tree Node selection eventTree Node selection event
14.42.6.TreeView CheckBoxTreeView CheckBox