DriveListBox, DirListBox and FileListBox Controls


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:

Drive property of the DriveListBox control




Working with the three list boxes together

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

Output: The output will vary from computer to computer.

program show the file name of a file




The Pattern property of FileListBox

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

Output: The output will vary from computer to computer.

program to show the path of a file using Drive, Path and FileName property

Download sample program: Photo Viewer