VB6 Tutorial 44: Working with Drive, Directory, and File list boxes

By | 2018-05-20

In this lesson, I’ll talk about the file system controls in VB6.

To work with the file system, the DriveListBox, DirListBox and FileListBox are used together that will access the files stored in the secondary memory of your computer. The DriveListBox shows all the drives on your computer, the DirListBox displays all the sub-directories of a given directory, and the FileListBox shows the files in a particular directory.

Most of the properties, events and methods are same as the ListBox and ComboBox controls. So the common things have not been explained in this section.

Selected Items

The Drive, Path and FileName properties return the selected item in the DriveListBox, DirListBox and FileListBox respectively. The Drive property returns the selected disk drive from your computer.

Setting the Drive

Example

Private Sub Form_Load()
    Drive1.Drive = "G"
End Sub

Output

Working with three listboxes

When the user selects a drive, this change should be reflected in DirListBox and when the user selects a directory, this change should be reflected in the FileListBox. Write the following code for that.

Example

Private Sub Dir1_Change()
    File1.Path = Dir1.Path
End Sub
Private Sub Drive1_Change()
    Dir1.Path = Drive1.Drive
End Sub

Showing the selected file name

To show only the selected file name, write the following code.

Example

This program will show only the name of the selected file.

Private Sub cmdShowFileName_Click()
    Print File1.FileName
End Sub
Private Sub Dir1_Change()
    File1.Path = Dir1.Path
End Sub
Private Sub Drive1_Change()
    Dir1.Path = Drive1.Drive
End Sub

The Pattern property

The Pattern property sets which files to be shown in the list. The default value is *.* (all files). You can specify which types of files to be shown. To enter multiple specifications, use semicolon as a separator. You can set the Pattern in run-time also.

Example

File1.Pattern = "*.bmp;*.ico;*.wmf;*.gif;*.jpg"

Showing the complete file path with file name

Example

Private Sub cmdShowFileName_Click()
    f = File1.Path
    If Right(f, 1) <> "\" Then
        f = File1.Path + "\"
    End If
    Print f + File1.FileName
End Sub
Private Sub Drive1_Change()
    Dir1.Path = Drive1.Drive
End Sub
Private Sub Dir1_Change()
    File1.Path = Dir1.Path
End Sub

I’ve attached an example project to this post, a rudimentary photo viewer. Take a look at the code driving the events to see how an event for one control updates a different control.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.