File, Folder and Drive List

This example shows you how to integrate the three file system controls: FileListBox, DirListBox and DriveListBox to create a browse for file dialog box

Simply add a FileListBox, called lstFiles, a DirListBox called lstFolders and a DriveListBox called lstDrives. Finally, add the code below to the form.

'
Dim mfMustExist As Integer
Dim mfCancelExit As Integer
Dim LastDrive As String

Private Sub lstFolders_Change()
    Static intBusy As Integer

    On Error Resume Next

    If intBusy = False Then
        intBusy = True
        ' Change the current directory
        ChDir lstFolders.Path
       
        If Err = 0 Then
            ' If an error does not occur, then
            ' update file and directory list
            lstFiles.Path = lstFolders.Path
            lstDrives.Drive = Left$(lstFolders.Path, 2)
        Else
            ' If an error occurs - cancel it
            Err = 0
        End If

        intBusy = False
    End If
End Sub

Private Sub lstDrives_Change()
    Dim Ans As String
    On Error Resume Next
Start:
    lstFiles.Path = lstFolders.Path
    lstFolders.Path = lstDrives.Drive
    ' If and error occurs
    Select Case Err
        Case 68
            ' Err = not accessable
            Ans = MsgBox(lstDrives.Drive & " is not accessible", vbRetryCancel + vbCritical)
            If Ans = vbRetry Then
                Err = 0
                GoTo Start
            Else
                ' Error reading drive, switch
                ' to last one
                lstDrives.Drive = LastDrive
            End If
        Case 0
            Exit Sub
        Case Else
            ' Unexpected error
            MsgBox "Unexpected Error " & Err & " : " & Error
            lstDrives.Drive = LastDrive
    End Select
End Sub

Private Sub Form_Load()
    ' set file list path
    lstFiles.Path = lstFolders.Path
    ' set last drive
    LastDrive = lstDrives.Drive
End Sub

You might also like...

Comments

James Crowley James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audience ...

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“I invented the term Object-Oriented, and I can tell you I did not have C++ in mind.” - Alan Kay