Writing a File Dialog Box
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:
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:
Drive Property (information from MSDN)
Returns or sets the selected drive at run time. Not available at design time.
drivelist.Drive [= drive]
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:
When setting this property:
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.
object.Path [= pathname]
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:
The preceding syntax changes the Drive property to a zero-length string ("").
Changing the value of Path has these effects:
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
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.
Quick searches: Site Search | Advanced Site Search
By using this site you agree to its terms and conditions
VB Explorer and VBExplorer.com are trademarks of Exhedra Solutions, Inc.