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

Home
VB.Net Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statements
5.Date Time
6.Class Module
7.Development
8.Collections
9.Generics
10.Attributes
11.Event
12.LINQ
13.Stream File
14.GUI
15.GUI Applications
16.Windows Presentation Foundation
17.2D Graphics
18.I18N Internationlization
19.Reflection
20.Regular Expressions
21.Security
22.Socket Network
23.Thread
24.Windows
25.XML
26.Database ADO.net
27.Design Patterns
VB.Net
VB.Net by API
VB.Net Tutorial » GUI » TreeView 
14.42.3.Build file tree recursively
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.EventArgsHandles ActTraverse.Click
        If (My.Computer.FileSystem.DirectoryExists(StartPath.Text= FalseThen
            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 NothingThen
                newDirectory = PathTree.Nodes.AddjustTheSubdirectory)
            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(32632)
        Me.ActTraverse.Name = "ActTraverse"
        Me.ActTraverse.Size = New System.Drawing.Size(7523)
        Me.ActTraverse.TabIndex = 2
        Me.ActTraverse.Text = "Traverse"
        Me.ActTraverse.UseVisualStyleBackColor = True
        '
        'StartPath
        '
        Me.StartPath.Location = New System.Drawing.Point(728)
        Me.StartPath.Name = "StartPath"
        Me.StartPath.Size = New System.Drawing.Size(32820)
        Me.StartPath.TabIndex = 1
        '
        'PathTree
        '
        Me.PathTree.Location = New System.Drawing.Point(7264)
        Me.PathTree.Name = "PathTree"
        Me.PathTree.Size = New System.Drawing.Size(329184)
        Me.PathTree.TabIndex = 4
        '
        'Label1
        '
        Me.Label1.AutoSize = True
        Me.Label1.Location = New System.Drawing.Point(810)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(5713)
        Me.Label1.TabIndex = 0
        Me.Label1.Text = "Start Path:"
        '
        'Label2
        '
        Me.Label2.AutoSize = True
        Me.Label2.Location = New System.Drawing.Point(866)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(5713)
        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(411260)
        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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.