I'm giving a user a CFileDialog
to save their work. One of the file types supported is CSV, but within CSV there are a couple variations (e.g., row-major, column-major, etc.). I know I can add controls to the dialog to allow such a choice but given that there's already an output file type selector I'd like to use that if possible.

- 1,985
- 15
- 33
1 Answers
I am going to assume that you know how to add mutiple file types to the CFileDialog
list as there are lots of articles about that.
Once the window has been dismissed you can consider using the GetOFN method. This returns a OPENFILENAME structure.
If you look closely at the information for this structure you will see:
nFilterIndex
Type: DWORD
The index of the currently selected filter in the File Types control. The buffer pointed to by
lpstrFilter
contains pairs of strings that define the filters. The first pair of strings has an index value of 1, the second pair 2, and so on. An index of zero indicates the custom filter specified bylpstrCustomFilter
. You can specify an index on input to indicate the initial filter description and filter pattern for the dialog box. When the user selects a file,nFilterIndex
returns the index of the currently displayed filter. IfnFilterIndex
is zero andlpstrCustomFilter
isNULL
, the system uses the first filter in thelpstrFilter
buffer. If all three members are zero orNULL
, the system does not use any filters and does not show any files in the file list control of the dialog box.
So, once the window is dismissed you can get the selected filter index value. Since you know what type of CSV file is associated with each index you know what to do.
Update
You can also initialize the CFileDialog
before it is displayed by modifying the same structure. For example:
dlgFiles.m_ofn.nFilterIndex=2;
You can set the filters etc. directly using this method and then display the window. Then afterwards, access the structure to get the selected index at the moment the window was dismissed.

- 1
- 1

- 17,769
- 16
- 66
- 164
-
2I'm not sure, what issue the OP is having exactly, but I believe it's worth a note, that a filter pattern (`"*.CSV"`) can show up in more than one filter string. It's not used as a key and need not be unique. – IInspectable Feb 19 '18 at 09:52
-
@IInspectable Exactly. It seems to me he will have atleast two keys, all with a file type of CSV. But one writes like this and the other like that. This is where the index can be used to fine tune which CSV format to write. – Andrew Truckle Feb 19 '18 at 09:57
-
1Haven't tried yet but that looks like a good solution to figure out which CSV format a user selected. One other angle though: I'll need to set that menu to the correct one--that is to say if it was loaded with CSV format #2 then writing format #2 would be the best default... – Swiss Frank Feb 19 '18 at 13:11
-
2That should be `nFilterIndex`, not `nFileExtension`. – IInspectable Feb 19 '18 at 14:44
-
2You can initialise the `OFN` structure before the window is displayed. Eg: `dlgExport.m_ofn.nFilterIndex=2` – Andrew Truckle Feb 19 '18 at 14:47