Knowledge Base |

# PRB: Default Extension Ignores File Type in VB Common Dialog

Article ID: 106682

Article Last Modified on 1/8/2003

APPLIES TO

- Microsoft Visual Basic 3.0 Professional Edition
- Microsoft Visual Basic 3.0 Professional Edition
- Microsoft Visual Basic 4.0 Professional Edition
- Microsoft Visual Basic 4.0 16-bit Enterprise Edition

This article was previously published under Q106682

## SYMPTOMS

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.

## CAUSE

This behavior is by design in the common dialog control in Visual Basic.

## WORKAROUND

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.

## STATUS

This behavior is by design. A change in the design is under review and will be considered for inclusion in a future release.

## MORE INFORMATION

NOTE: In Visual Basic 4.0, the common dialog's name property must be changed from CommonDialog1 to CMDialog1.

### Steps to Reproduce Behavior

1. Start a new project in Visual Basic. Form1 is created by default.
2. Add a common dialog control to Form1. This requires CMDIALOG.VBX to be loaded in Visual Basic. You can load CMDIALOG.VBX automatically through your AUTOLOAD.MAK file or by choosing Add File from the File menu.
3. Add the following to the Form Load event code:

          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

4. Start the program or press the F5 key. The Open dialog now displays.
5. Under List Files of Type, select Basic Files *.Bas. Under File Name, enter a filename such as TESTFILE without an extension and without a period. Click OK.
   The debug window now shows the following limitations:
   1. The CMDialog1.FilterIndex property keeps its value of 1. CMDialog1.FilterIndex does not change in response to your changing the file type in the Open dialog box. This is by design in the common dialog custom control in Visual Basic.
   2. The CMDialog1.Filename property returns C:\VB\TESTFILE.TXT, which is the filename you entered plus the default extension .TXT. Notice that the program cannot detect the file type you chose in the Open dialog box. The default extension .TXT set by the DefaultExt property is independent of changes under List Files of Type. These are design limitations in the common dialog custom control in Visual Basic.

NOTE: File names that you enter with an extension keep that extension as desired.

### Workaround for Windows API Programmers

Visual Basic's common dialog custom controls for Open and Save As pass their FilterIndex property to the Windows API function GetOpenFileName. GetOpenFileName is located in the Windows COMMDLG.DLL file. However, Visual Basic ignores the nFilterIndex value that the GetOpenFileName function returns. By design, your Visual Basic program cannot access the structure returned by the GetOpenFileName function, even by calling API routines.
You can write your own DLL routine in C to call the Windows common dialog routines located in COMMDLG.DLL. Then call this DLL from Visual Basic. The following documentation from the Windows Software Development Kit (SDK) explains how to use the nFilterIndex element of the structure passed to GetOpenFileName:

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: kbprb KB106682