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

Posted On 2018-05-02 by VB6Boy
Keywords:
Tags: VB6 Tutorials 
Views: 49

Title Uploaded Size
FileBrowser.zip 5/2/2018 4:31:38 PM 2,031


Looking for the more of the VB6 Tutorial? Here is the tutorial index.

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

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

rudimentary photo viewer

Looking for the more of the VB6 Tutorial? Here is the tutorial index.


About the Author

has posted a total of 78 articles.


Comments On This Post

No comments on this post yet!


Do you have a thought relating to this post? You can post your comment here. If you have an unrelated question, you can use the Q&A section to ask it.

Or you can drop a note to the administrators if you're not sure where you should post.


Your IP address is:54.166.212.152

Before you can post, you need to prove you are human. If you log in, this test goes away.


Code Links