Directory Tree : TreeView « GUI « VB.Net






Directory Tree

  
Imports System.IO

Imports System.Windows.Forms

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

    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.SplitContainer1 = New System.Windows.Forms.SplitContainer
        Me.treeFiles = New System.Windows.Forms.TreeView
        Me.lstFiles = New System.Windows.Forms.ListBox
        Me.SplitContainer1.Panel1.SuspendLayout()
        Me.SplitContainer1.Panel2.SuspendLayout()
        Me.SplitContainer1.SuspendLayout()
        Me.SuspendLayout()
        '
        'SplitContainer1
        '
        Me.SplitContainer1.Dock = System.Windows.Forms.DockStyle.Fill
        Me.SplitContainer1.Location = New System.Drawing.Point(5, 5)
        Me.SplitContainer1.Name = "SplitContainer1"
        '
        'SplitContainer1.Panel1
        '
        Me.SplitContainer1.Panel1.Controls.Add(Me.treeFiles)
        '
        'SplitContainer1.Panel2
        '
        Me.SplitContainer1.Panel2.Controls.Add(Me.lstFiles)
        Me.SplitContainer1.Size = New System.Drawing.Size(429, 295)
        Me.SplitContainer1.SplitterDistance = 143
        Me.SplitContainer1.TabIndex = 0
        '
        'treeFiles
        '
        Me.treeFiles.Dock = System.Windows.Forms.DockStyle.Fill
        Me.treeFiles.Location = New System.Drawing.Point(0, 0)
        Me.treeFiles.Name = "treeFiles"
        Me.treeFiles.Size = New System.Drawing.Size(143, 295)
        Me.treeFiles.TabIndex = 1
        '
        Me.lstFiles.Dock = System.Windows.Forms.DockStyle.Fill
        Me.lstFiles.IntegralHeight = False
        Me.lstFiles.Location = New System.Drawing.Point(0, 0)
        Me.lstFiles.Name = "lstFiles"
        Me.lstFiles.Size = New System.Drawing.Size(282, 295)
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(439, 305)
        Me.Controls.Add(Me.SplitContainer1)
        Me.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Padding = New System.Windows.Forms.Padding(5)
        Me.Text = "File Browser"
        Me.SplitContainer1.Panel1.ResumeLayout(False)
        Me.SplitContainer1.Panel2.ResumeLayout(False)
        Me.SplitContainer1.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub
    Friend WithEvents SplitContainer1 As System.Windows.Forms.SplitContainer
    Friend WithEvents treeFiles As System.Windows.Forms.TreeView
    Friend WithEvents lstFiles As System.Windows.Forms.ListBox

    Private Sub Fill(ByVal Dir As DirectoryInfo, ByVal DirNode As TreeNode)
        Try
            For Each DirItem As DirectoryInfo In Dir.GetDirectories
                Dim NewNode As New TreeNode(DirItem.Name)
                DirNode.Nodes.Add(NewNode)
                NewNode.Nodes.Add("*")
            Next
        Catch
        End Try
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim RootDir As New DirectoryInfo("c:\")
        Dim RootNode As New TreeNode("c:\")
        treeFiles.Nodes.Add(RootNode)
        Fill(RootDir, RootNode)
    End Sub

    Private Sub treeFiles_BeforeExpand(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles treeFiles.BeforeExpand
        If e.Node.Nodes.Count > 0 Then
            If e.Node.FirstNode.Text = "*" Then
                e.Node.FirstNode.Remove()
                Dim DirectoryToFill As New DirectoryInfo(e.Node.FullPath)
                Fill(DirectoryToFill, e.Node)
            End If
        End If
    End Sub
End Class

   
    
  








Related examples in the same category

1.Tree DemoTree Demo
2.Add Mouse Click event to a TreeView: display full pathAdd Mouse Click event to a TreeView: display full path
3.Add Node and Leap to a TreeViewAdd Node and Leap to a TreeView
4.Custom Tree ViewCustom Tree View
5.Use Tree View to display directoryUse Tree View to display directory
6.Using a TreeView to display the directory structureUsing a TreeView to display the directory structure
7.TreeView: Resource Browser
8.Relational TreeView