Writing a File Dialog Box
By Anne-Marie Wright
Although there is a common dialog box control with VB, it is
not always what we want. In this article we will be
emulating the file handling part of the old windows 3.1 file
dialog box. You can modify this example to suit your particular needs.
To start with we need to create a form with a drivelist, directorylist,
filelist, label and a textbox contol, with 2 buttons. (See picture
below)

Set the control up as follows:
Form
Label
TextBox
DirectoryList
DriveList
FileList
Command1 Button
Command2 Button
Now we have the form setup, we need to get the controls interacting
with each other.
To link the controls together we will use the following properties:
- DriveList - Drive
- DirectoryList - Path
- FileList - Path
Drive Property (information from MSDN)
Returns or sets the selected drive at run time. Not available
at design time.
Syntax
drivelist.Drive [= drive]
Remarks
The valid drives for the Drive property include all drives
present in or connected to the system when the control is created
or refreshed at run time. The default setting of the Drive property
is the current drive.
When reading this property setting, the selected drive is returned
in one of the following formats:
- Floppy disks "a:" or "b:", and
so on
- Fixed media "c: [volume id]"
- Network connections "x: \\server\share"
When setting this property:
- Only the first character of the string is significant (the
string isn't case-sensitive).
- Changing the setting for the Drive property invokes a Change
event.
- Selecting a drive that isn't present causes an error.
- Setting this property also regenerates the drive list, providing
a way in code to track network connections added since the control
was created.
If the FileName property is set to a qualified network path
without a drive designation, the value of the Drive property is
a zero-length string (""), no drive is selected, and
the ListIndex property setting is 1.
Note: The Drive property returns a different value from the
ListIndex property, which returns the list box selection.
(MSDN April 2000)
Path Property (information from MSDN)
Returns or sets the current path. Not available at design time.
For the App object, read-only at run time.
Syntax
object.Path [= pathname]
Remarks
The value of the Path property is a string indicating a path,
such as C:\Ob or C:\Windows\System. For a DirListBox or FileListBox
control, the default is the current path when the control is created
at run time. For the App object, Path specifies the path of the
project .VBP file when running the application from the development
environment or the path of the .exe file when running the application
as an executable file.
Use this property when building an application's file-browsing
and manipulation capabilities. Setting the Path property has effects
on a control similar to the MS-DOS chdir command relative
paths are allowed with or without a drive specification. Specifying
only a drive with a colon (:) selects the current directory on
that drive.
The Path property can also be set to a qualified network path
without a drive connection using the following syntax:
\\servername\sharename\path
The preceding syntax changes the Drive property to a zero-length
string ("").
Changing the value of Path has these effects:
- For a DirListBox control, generates a Change event.
- For a FileListBox control, generates a PathChange event.
Note: For DirListBox, the return value of Path is different
from that of List(ListIndex), which returns only the selection.
(MSDN April 2000)
Now we have had a look at the properties below is the code
that links it all together
Option Explicit
Private Sub DirDialog_Change()
'Set the filelist path to the same path
filDialog.Path = DirDialog.Path
End Sub
Private Sub drvDialog_Change()
'Set the directorylist path to the same drive
DirDialog.Path = drvDialog.Drive
End Sub
Private Sub filDialog_PathChange()
'Show the path in the forms caption
Me.Caption = "Dialog Box - [" & filDialog.Path & "]"
End Sub
If we assume that you have changed the drive in the drivelist
control (drvDialog), this is what happens.
|
Change the drive to D: |
This causes the drivelist control's 'change' event to be run |
|
drvDialog_Change |
When the directorylist path is set, it causes a 'change' event
in the directorylist control |
|
dirDialog_Change |
When the filelist path is set, it causes a 'pathchange' event
in filelist control |
|
filDialog_PathChange |
The caption on the form is set to display the path |
Now all we need to do to finish the dialog box is select the
filename and program the buttons
Private Sub filDialog_Click()
'Display the name in the textbox
txtFilename.Text = filDialog.FileName
End Sub
Private Sub cmdCancel_Click()
MsgBox "CANCELLED"
Unload Me
End Sub
Private Sub cmdOK_Click()
Dim strMessage
'Display various elements that can be returned from the form
strMessage = "Drive: " & drvDialog.Drive
strMessage = strMessage & vbCr & "Path: " & DirDialog.Path
strMessage = strMessage & vbCr & "Filename: " & filDialog.FileName
strMessage = strMessage & vbCr & "The lot: "
'Check the path has a paragraph mark at the end
If Right(filDialog.Path, 1) = "\" Then
strMessage = strMessage & filDialog.Path & filDialog.FileName
Else
strMessage = strMessage & filDialog.Path & "\" & filDialog.FileName
End If
MsgBox strMessage
Unload Me
End Sub
We have to check the file path because if you
are looking a the root of a drive then the path will have a directory
character at the end of the path ('\').
Now we have created a simple file dialog box. You can download the completed sample project here.