Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 39,449 times

Contents

Related Categories

Drive Combo, Folder and File List Controls - Putting it all together

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

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

  • Houston, we have a problem...

    Posted by HyperHacker on 28 Jul 2003

    When I try to set the drive list's drive back to the previous one, it shows me that drive in the dir/file boxes, but the drive selector remains at the drive the user selected. Any way to fix it? (Usin...

  • Thank you VERY much!

    Posted by Knoj on 30 May 2003

    I am a beginner, and I found your article VERY easy to use, and it saved me alot of time on my project! Thank you again, and I have a small possibly un-nessary, but usefull addition to one line of yo...