Drive Combo, Folder and File List Controls

Putting it all together

It is very easy to write simple code to utilise all these three controls. The following code integrates the three. When you change the drive it updates the file and directory list, and when you change the folder it lists the files it contains.

' This event occurs when a new drive is selected
Sub Drive1_Change()
    ' change the folder path to the new drive
    Dir1.Path = Drive1.Drive
    ' change the file path
    File1.Path = Drive1.Drive
End Sub

' This event occurs when a new directory is selected
Sub Dir1_Change()
    ' change the file path
    File1.Path = Dir1.Path
End Sub

As you can see, this requires very little code. However one thing you need to take into account is that sometimes the user will select the floppy drive, forgetting to put in a floppy disk. With the code above, your application would crash. To allow for these events, you need to write a bit more code. The following code does the same above, just does not crash when an invalid drive or folder is selected.

Public OldDrive As String
' This event occurs when a new drive is selected
Sub Drive1_Change()
    ' Trap an error if it occurs
    On Error GoTo driveerr
    ' change the folder path to the new drive
    Dir1.Path = Drive1.Drive
    ' change the file path
    File1.Path = Drive1.Drive
    ' Save the new drive name
    OldDrive = Drive1.Drive
    'Exit procedure, no error has occured
    Exit Sub
driveerr:
    ' error handling procedure
    Select Case Err
        Case 68
            MsgBox "Drive not accessable"
            ' Switch back to the previosu drive
            Drive1.Drive = OldDrive
            Exit Sub
        Case Else
            MsgBox "Unexpected Error." & Err & " : " & Error
            Drive1.Drive = OldDrive
            Exit Sub
    End Select
End Sub

' This event occurs when a new directory is selected
Sub Dir1_Change()
    ' change the file path
    File1.Path = Dir1.Path
End Sub

Private Sub Form_Load()
    ' Save the drive name
    OldDrive = Drive1.Drive
End Sub

You might also like...

Comments

About the author

James Crowley

James Crowley United Kingdom

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 audien...

Interested in writing for us? Find out more.

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.

“Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away.” - Antoine de Saint Exupéry