CFileDialog multiple CSV file types

394 Views Asked by At

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

There are 1 best solutions below

5
Andrew Truckle On

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 by lpstrCustomFilter. 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. If nFilterIndex is zero and lpstrCustomFilter is NULL, the system uses the first filter in the lpstrFilter buffer. If all three members are zero or NULL, 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.