PRB: Default Extension Ignores File Type in VB Common DialogID: Q106682
|
The common dialog custom control (CMDIALOG.VBX) cannot determine which file
type you choose in the Open or Save As dialog box under List Files of Type.
Your chosen file type correctly displays existing files of that type and
filters out other files. However, Visual Basic code cannot detect which
file type you chose.
Also, the default file name extension set by the DefaultExt property is not
affected by changes you make under List Files of Type. As a result, a file
name that you enter without an extension will take the extension of
DefaultExt instead of your choice under List Files of Type.
The above behavior of File Open and File Save As is different from many
other Windows applications, such as Microsoft Excel. Excel determines which
file-type filter you choose and automatically appends that extension to
any file name that you may enter without an extension.
This behavior is by design in the common dialog control in Visual Basic.
Instead of using Visual Basic's common dialog custom control, you can write your own DLL routine in C to call the Windows common dialog routines located in COMMDLG.DLL. Then you can call that DLL from Visual Basic.
This behavior is by design. A change in the design is under review and will be considered for inclusion in a future release.
NOTE: In Visual Basic 4.0, the common dialog's name property must be
changed from CommonDialog1 to CMDialog1.
Sub Form_Load ()
' Enter the following two lines as one, single line:
CMDialog1.Filter = "Text Files *.Txt|*.Txt|Basic Files *.Bas|*.Bas|
All Files *.*|*.*"
CMDialog1.FilterIndex = 1 'Sets default filter to *.txt.
CMDialog1.DefaultExt = "TXT" 'Default extension if you enter none.
' In the dialog box, the default extension will be applied only if you
' enter the filename with no period. If you type the file name with
' a period and no extension (such as FILEX.), then CMDialog1.Filename
' always returns a blank extension.
' Set the common dialog Action property to 1 to execute the File Open
' dialog or 2 to execute the File Save As dialog:
CMDialog1.Action = 1 ' 1=Invokes the File Open common dialog box.
' Limitation: The value of FilterIndex doesn't change even if you change
' the file type in the Open common dialog box:
Debug.Print CMDialog1.FilterIndex
' The Filename property displays the filename and path that you entered
' in the Open dialog:
Debug.Print CMDialog1.Filename 'Prints filename with path prefix
' Debug.Print CMDialog1.Filetitle 'Prints filename without path
End Sub
nFilterIndex:
Specifies an index into the buffer pointed to by the lpstrFiler member. The system uses the index value to obtain a pair of strings to use as the initial filter description and filter pattern for the dialog box. The first pair of strings has an index value of 1. When the user chooses the OK button to close the dialog box, the system copies the index of the selected filter strings into this location. If the nFilterIndex member is 0, the filter in the buffer pointed to by the lpstrCustomFilter member is used. If the nFilterIndex member is 0 and the lpstrCustomFilter member is NULL, the system uses the first filter in the buffer pointed to by the lpstrFilter member. If each of the three members is either 0 or NULL, the system does not use any filters and does not show any files in the File Name list box of the dialog box.
Additional query words: 2.00 3.00 4.00 vb416 vb4win
Keywords : kbcode PrgCtrlsCus
Version : 2.00 3.00 4.00
Platform : WINDOWS
Issue type :
Last Reviewed: June 16, 1999