File Treeview : File Tree « GUI Applications « VB.Net Tutorial

File Treeview
Imports System.ComponentModel
Imports System.Drawing
Imports System.Runtime.InteropServices
Imports System.Windows.Forms
Imports System.Windows.Forms.Design
Imports System.IO

public class TreeViewFileFolder
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class

Public Class Form1
    Inherits System.Windows.Forms.Form

    Public Sub New()

    End Sub
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
            End If
        End If
    End Sub

    Private components As System.ComponentModel.IContainer
    Friend WithEvents TreeView1 As System.Windows.Forms.TreeView
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.TreeView1 = New System.Windows.Forms.TreeView
        Me.TreeView1.Location = New System.Drawing.Point(16, 15)
        Me.TreeView1.Name = "TreeView1"
        Me.TreeView1.Size = New System.Drawing.Size(544, 273)
        Me.TreeView1.TabIndex = 0
        Me.AutoScaleBaseSize = New System.Drawing.Size(8, 18)
        Me.ClientSize = New System.Drawing.Size(587, 309)

    End Sub

    Public Function BuildDirectory(ByVal aNode As TreeNode) As String
        Dim theNode As TreeNode = aNode
        Dim strDir As String = ""
        While Not (theNode Is Nothing)
            If theNode.Text.Substring((theNode.Text.Length - 1)) <> "\" Then
                strDir = "\" + strDir
            End If
            strDir = theNode.Text + strDir
            theNode = theNode.Parent
        End While
        Return strDir
    End Function
    Public Sub PopulateNode(ByVal aNode As TreeNode, ByVal strDir As String)
        Me.Cursor = Cursors.WaitCursor
        Dim Dir As Directory 
        Dim count As Integer = 0
        Dim M_STRfILTER = "*.*"
        Dim i As Integer
        For i = 0 To (Dir.GetDirectories(strDir).Length) - 1
            Dim ChildNode As New TreeNode(Dir.GetDirectories(strDir)(i).ToString, 1, 0)
            i = aNode.Nodes.Add(ChildNode)
            count += 1
        Next i

        For i = 0 To (Dir.GetFiles(strDir, M_STRfILTER).Length) - 1
            Dim ChildNode As New TreeNode(Dir.GetFiles(strDir, M_STRfILTER)(i).ToString, 2, 2)
            i = aNode.Nodes.Add(ChildNode)
        Next i
        Me.Cursor = Cursors.Arrow
    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim oNode As New System.Windows.Forms.TreeNode()
            oNode.ImageIndex = 0
            oNode.SelectedImageIndex = 0
            oNode.Text = "C:\"
        Catch ex As Exception
            Console.WriteLine("Cannot create initial node:" & ex.ToString)
        End Try
    End Sub

    Private Sub TreeView1_AfterSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
        Text = e.Node.Text
        If Directory.Exists(e.Node.Text) = True Then PopulateNode(e.Node, e.Node.Text)
        If File.Exists(e.Node.Text) = True Then MsgBox(e.Node.Text)
    End Sub

End Class

15.12.File Tree
15.12.1.Generate File treeGenerate File tree
15.12.2.File TreeviewFile Treeview