We need you!

We're working hard on the next version of Developer Fusion. Let us know what you think we should be up to!

Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Rated
Read 24,374 times

Related Categories

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

James first started writing tutorials on Visual Basic in 1999 whilst starting this website (then known as VB Web). Since then, the site has grown rapidly, and James has written numerous tutorials, articles and reviews on VB, PHP, ASP and C#. In October 2003, James formed the company Developer Fusion Ltd, which owns this website, and also offers various development services. In his spare time, he's a 3rd year undergraduate studying Computer Science in the UK. He's also a Visual Basic MVP.

Comments