Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
If I have a CFileDialog with multiple file extensions (filters), is there a way to have it change the filename change its extension depending on the selected filter ?

if I have (simplified) the following filter "*.txt;*.xls;*.jpg;*.cpp" and in the file dialog I have a default file name "myfile.txt"; when I change the filter from ".txt" to ".jpg" the filename does not change to "myfile.jpg".

Is this a supported feature of the CFileDialog on Vista ?

I tried doing like I did (pre-vista) but the new file dialog does not support the same features that the old one did (i.e. getting the edit control (GetDlgItem(idt1) or SetControlText(edt1, myString) ).

If I change the file dialog style to be not vista with the bVistaStyle flag of the CFileDialog constructor; then it's working as expected. but not the other way around.

I'd like to keep the vista style dialogs (also easier to be Win7/8 compliant)

Any Ideas ?


Thanks.
Posted
Updated 3-Feb-17 2:20am

1 solution

For your default file name, don't supply an extension. Instead of "myfile.txt" simply use "myfile". This way (if your filter is set up correctly) the proper extension will be applied when the user presses OK.

A typical filter string would be more like "*.txt\0*.txt\0*.xls\0*.xls\0*.jpg\0*.jpg\0*.cpp\0*.cpp\0\0"

Hope that helps.
 
Share this answer
 
v2
Comments
Maximilien 21-Jul-11 9:25am    
Nope, it I type "toto" (no extension) in the file name field and press OK, the filename will still be "toto"; if I change filter the filename still stays "toto".
As for the filter string, that's why I wrote "simplified"...
Gast128 14-Aug-19 4:14am    
It works @ my PC only if the lpszDefExt is also filled in with the correct extension.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900