cDialog class
Shows the Windows Open, Save As and Browse For Folder dialogs, builds the file-type list of the first two, takes file paths apart, and opens a file in the program that Windows associates with it.
Properties set the options of a dialog before it is shown. ShowOpen, ShowSave and ShowBrowseForFolder show the dialogs and return the path that the user chose. A cancelled dialog returns an empty string. ShowOpen and ShowSave also store the path in FileName.
Dim dlg As New cDialog
Dim path As String
dlg.DialogTitle = "Open a text file"
dlg.SetCommonFilters
path = dlg.ShowOpen()
If Len(path) > 0 Then
Debug.Print "Chosen: " & path
Debug.Print "Name: " & dlg.GetFileName(path)
End If
The options are the members and flags of the Win32 structures that the dialogs take: OPENFILENAME for ShowOpen (GetOpenFileNameW) and ShowSave (GetSaveFileNameW), and BROWSEINFO for ShowBrowseForFolder (SHBrowseForFolder). Each property page names the flag it sets and the dialogs in which it takes effect, and the table below lists them all.
Note
The property names follow VB6’s CommonDialog control, but cDialog is a class with a smaller set of members. It has no Flags property (each option is a Boolean property of its own), no FilterIndex (the first filter is always selected, and the filter that the user chose is not reported), no CancelError (a cancelled dialog returns an empty string), no FileTitle or MaxFileSize, and no owner window. InitialDir is the control’s InitDir. ShowOpen returns the file name, as well as storing it in FileName, and there is no Color, Font or Print dialog. The CommonDialog class of the VBComDlg package has Flags, FilterIndex, CancelError, FileTitle, MaxFileSize and those dialogs.
Warning
ShowBrowseForFolder works in 32-bit programs only. In a 64-bit program it ends the program with an access violation before any dialog appears, because the class declares the handles, pointers and item list of that dialog as Long.
Warning
With MultiSelect True and several files selected, ShowOpen returns only the folder of the files, as a String, and the names of the files are lost. No error is raised.
Defaults
A new cDialog starts with these values, and Reset restores them.
| Property | Starting value |
|---|---|
| DialogTitle, InitialDir, DefaultExt, FileName, Filter | An empty string |
| MultiSelect, ReadOnly, NoChangeDir, CreatePrompt | False |
| OverwritePrompt, PathMustExist, FileMustExist, HideReadOnly, NewDialogStyle | True |
Dim dlg As New cDialog
Debug.Print dlg.MultiSelect ' False
Debug.Print dlg.FileMustExist ' True
Debug.Print dlg.NewDialogStyle ' True
Debug.Print Len(dlg.Filter) ' 0
Which dialog uses which property
yes means that the class passes the value to Windows for that dialog. A property marked ignored is stored and returned, and the dialog does not receive it. ShowOpen and ShowSave read and write FileName; ShowBrowseForFolder writes InitialDir when the user chooses a folder, and does not read it.
| Property | Win32 member or flag | ShowOpen | ShowSave | ShowBrowseForFolder |
|---|---|---|---|---|
| DialogTitle | lpstrTitle, lpszTitle | yes | yes | yes |
| InitialDir | lpstrInitialDir | yes | yes | written, not read |
| DefaultExt | lpstrDefExt | yes | yes | ignored |
| FileName | lpstrFile | read and written | read and written | ignored |
| Filter | lpstrFilter | yes | yes | ignored |
| MultiSelect | OFN_ALLOWMULTISELECT | yes | ignored | ignored |
| OverwritePrompt | OFN_OVERWRITEPROMPT | yes | yes | ignored |
| PathMustExist | OFN_PATHMUSTEXIST | yes | yes | ignored |
| FileMustExist | OFN_FILEMUSTEXIST | yes | ignored | ignored |
| HideReadOnly | OFN_HIDEREADONLY | yes | yes | ignored |
| ReadOnly | OFN_READONLY | yes | ignored | ignored |
| NoChangeDir | OFN_NOCHANGEDIR | yes | yes | ignored |
| CreatePrompt | OFN_CREATEPROMPT | yes | ignored | ignored |
| NewDialogStyle | BIF_USENEWUI | ignored | ignored | yes |
Windows documents some of these flags for one kind of dialog only. OFN_OVERWRITEPROMPT is documented for Save As, and OFN_FILEMUSTEXIST for Open. The class passes OverwritePrompt to ShowOpen all the same.
Settings without a property
Some settings cannot be changed.
- ShowOpen and ShowSave always set
OFN_LONGNAMES,OFN_EXPLORERandOFN_ENABLESIZING. Windows documents the effect ofOFN_LONGNAMESandOFN_ENABLESIZINGfor old-style dialogs, hooks and templates, none of which the class uses.OFN_EXPLORERselects the layout of the buffer that a multiple selection returns (MultiSelect). - ShowOpen and ShowSave always select the first pair of the Filter list (
nFilterIndexis 1) and do not report the pair that the user chose. - ShowOpen and ShowSave pass no hook procedure, no custom template and no custom filter. The flags that the dialog returns are not read, so the choice of Open as read-only (see HideReadOnly) is not available after the dialog closes.
- ShowBrowseForFolder always sets
BIF_RETURNONLYFSDIRS, so only a folder of the file system can be chosen, and passes no root folder and no callback. - No dialog has an owner window: the class passes 0 for
hwndOwnerin both structures.
Properties
- CreatePrompt – asks before the Open dialog accepts a file that does not exist
- DefaultExt – the extension that Windows adds to a file name typed without one
- DialogTitle – the text that the dialogs show as their title
- FileMustExist – lets the Open dialog accept only the name of an existing file
- FileName – the file name shown when a dialog opens, and the path that the last Open or Save As dialog returned
- Filter – the file-type list of the Open and Save As dialogs
- HideReadOnly – hides the read-only choice of the Open dialog
- InitialDir – the folder in which the Open and Save As dialogs start
- MultiSelect – lets the Open dialog select more than one file
- NewDialogStyle – selects the resizable Browse For Folder dialog with an edit box
- NoChangeDir – keeps the current folder of the program as it was
- OverwritePrompt – asks before the Save As dialog accepts the name of an existing file
- PathMustExist – lets the Open and Save As dialogs accept only paths that exist
- ReadOnly – sets the read-only flag of the Open dialog, which has no visible effect
Methods
- AddFilter – appends a description and a pattern to the filter
- ClearFilter – empties the filter
- GetFileExtension – returns the part of a path from its last period
- GetFileName – returns the part of a path after its last backslash
- GetFileNameWithoutExt – returns the file name of a path without its extension
- GetFilePath – returns the part of a path before its last backslash
- OpenFile – runs a shell verb, by default
open, on a file or a program - Reset – restores every property to its starting value
- SelectAndOpenFile – shows the Open dialog and opens the chosen file or files
- SetCodeFilters – replaces the filter with one for source code files
- SetCommonFilters – replaces the filter with Text Files and All Files
- SetDocumentFilters – replaces the filter with one for office documents
- SetImageFilters – replaces the filter with one for image files
- ShowBrowseForFolder – shows the Browse For Folder dialog and returns the chosen folder
- ShowOpen – shows the Open dialog and returns the chosen file
- ShowSave – shows the Save As dialog and returns the chosen file name
See Also
- TBMANLIB_Dialog package – overview
- CommonDialog class – the fuller alternative, with flags, a filter index, cancel errors and the Color, Font and Print dialogs