Link to home
Start Free TrialLog in
Avatar of MacSverre
MacSverre

asked on

Common Dialog/Save -- Selected file format?

In an application I'm making in VB 4 Pro, I use the CommonDialog's FileSave method to present a "save file" dialog box. The file type combo box has two choices, *.dwg and *.dxf.



Now, in Windows 95 the file type selection works as I thought it would; the appropriate (ie. the selected ) file suffix is added to the file name. But in Windows 3.1, it isn't. And I can't find any (other) way to check what file format the user selected. Can anyone help me out on this one?





Thanks in advance,



MacSverre
Avatar of ChrisLewis
ChrisLewis

The common dialog control is nto very restrictive.  You can basically enter any extention that you want into the dialog, and the dialog will just go on it's merry way, returning whatever the user typed.

Here's a solution.

After the Common dialog has been processed, use the FilterIndex to figure out what filter the user has selected.  Compare that with the FileTitle property to see if the user has indeed typed the same extention that they have selected.

+-----+
'set you Common dialog stuff here
CommonDlg1.SHowSave
Select case commondlg1.FilterIndex
  Case 0 'DWG
    strCompare = ".DWG"
  Case 1 'DXF
    strCompare = ".DXF"
  CASE ELSE
END SELECT
 
If RIGHT$(commondlg1.FileTitle,4) ,. strCompare THEN
  MSgbox "Invalid file name"
  'or fix the extention
  'Cancel Save
ENDIF
+-----+


Hope this helps

Chris
Avatar of MacSverre

ASKER

I'm afraid this doesn't work. The filterindex property is just there for setting the default file type in a list of file types. If you read it, it returns whatever you set it to in code (or in the properties window). If you haven't set it at all, it returns zero.



Anyone else?
Why don't you just check the filename the dialog returns and check the terminal three characters?  cut them off with the "right" function, then you know which extension was chosen.  Hope this helps...
Sorry, but if you read my original question, you can see that my problem is that Win 3.1 doesn't add the extension of the selected file format. That makes it kinda hard to check the suffix...

Anyone else?
Interesting.  This works in VB 5 with out any problems.  VB4's help file says that FilterIndex "Returns or sets a default filter"

I did try it in VB 4/16 and it doesn't work.  

You could try DefaultEXT.  DefaultEXT will set a default extension, but ignores the filter, so you only get one default.

Obviously, you cannot change to VB5 if you're in a Win 3.x environment.

This is what I turned up in the MS KB on FilterIndex:

Article ID: Q106682
PRB: Default Extension Ignores File Type in VB Common Dialog

Essentially, MS says that it screwed up, and that File type does not override DefaultExt, and it's not like anything else in Windows.

It suggests writing a DLL to wrap around the CommonDialog functions in COMMDLG.DLL.

From Q106682:
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.


ASKER CERTIFIED SOLUTION
Avatar of ChrisLewis
ChrisLewis

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you very much! I've done a "work-a-round" that I will use in my current project, but I _will_ use this in later projects.

About the extension: It doesn't really matter to me if the user types in a different extension, as long a I can check which file format was selected.

Again, thanks!



   MacSverre