Link to home
Start Free TrialLog in
Avatar of SpringMVC
SpringMVC

asked on

CFileDialog on Windows 7 problem.

Hello.
I am facing a weird problem.
If I try to create a file using CFileDialog on Windows 7, the "Save File Dialog", which asks create file or not, appears multiple times ( 2 or 3 ).
For example, use case is: CFileDialog appears, I enter some name and click "Open". If no file exists with such name, the "Save File Dialog" ( with OK and Cancel buttons) asking create file or not will be prompted 2 or 3 times. NB!! This "Save File Dialog" is prompted by CFileDialog inside DoModal function.
The code is simple.
P.S Sorry for my english :)

CFileDialog dlgFile(TRUE,0,0,OFN_CREATEPROMPT);
dlgFile.DoModal();

Open in new window

Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Does this make any difference ?
CFileDialog dlgFile(TRUE);

or
CFileDialog dlgFile(TRUE,NULL, NULL,OFN_CREATEPROMPT);

Avatar of SpringMVC
SpringMVC

ASKER

CFileDialog dlgFile(TRUE); - works fine, but it's not behaviour I want, because "Save File Dialog" dialog doesn't appear. I still need it to appear, but only 1 time, not 2 or 3.
CFileDialog dlgFile(TRUE,NULL, NULL,OFN_CREATEPROMPT); - tried, no difference, still 3 times. I think, this is the same, as
CFileDialog dlgFile(TRUE,0,0,OFN_CREATEPROMPT);, because #define NULL 0, so NULL=0.
>>but it's not behaviour I want, because "Save File Dialog" dialog doesn't appear.

do you mean you want the following:
CFileDialog dlgFile(FALSE);
In other words, I need OFN_CREATEPROMPT flag
No, I mean, I want open file dialog ( so CFileDialog dlgFile(TRUE);), but with OFN_CREATEPROMPT flag.
>>In other words, I need OFN_CREATEPROMPT flag

CFileDialog dlgFile(TRUE);
This will search for a file to OPEN, for reading, why do you need the OFN_CREATEPROMPT for that?

If you want to SAVE to a file then you need to use FALSE as the first parameter in the constructor.
ps.  Your original code only opens the CFileDialog once here in a test.  Can you post how you are using it
Hmm.
I don't want to SAVE file, I want to open. But if user enters a name and no file with such name exist, I want OFN_CREATEPROMPT flag, so user will be prompted with dialog ( something like "File does not exist, do you want to create it" ). If user presses No - execution returns to CFileDialog, and this MessageBox disappears - that's correct. But if user presses Yes, in Windows XP - everything works fine, file is NOT created, and DoModal returns IDOK, and this MessageBox disappears. in Windows 7, for some reason, this MessageBox ( again something like "File does not exist, do you want to create it") appears 2 or 3 times, all of them user must press OK button.

temp.jpg
The problem is, that this dialog must appear only ONE time ( and so it does in Windows XP ), but in Windows 7 - it appears 2 or 3 times.
P.S My first post is a little bit incorrect, sorry for confusion, I don't want to Save dialog, I want to open it, and if user enters file, which doesn't exist, I just want to prompt a suitable MessageBox ( shown above ). I realize, that CFileDialog ( TRUE ) - won't create file.
I've just typed a file name directly into the edit box and hit OK (using your original code) - only one prompt.  I'm using windows 7.

Please post the complete code you are using.
I have created a new MFC project from scratch.
In the function CFileDlgTestApp::OnAppAbout() I made small changes: I included
CFileDialog dlgFile(TRUE,0,0,OFN_CREATEPROMPT);
dlgFile.DoModal();
return;
So then I click "About", this code executes and dialog is prompted multiple times.

Tested on two window 7 machines: first computer prompted 3 times, second prompted 2 times. On WinXP - everything works fine, one prompt.
void CFileDlgTestApp::OnAppAbout()
{
	CFileDialog dlgFile(TRUE,0,0,OFN_CREATEPROMPT);
	dlgFile.DoModal();
	return;

	CAboutDlg aboutDlg;
	aboutDlg.DoModal();
}

Open in new window

On mine (win 7, 32 bit) just one prompt.
If I attach here an exe file, can you test it?
Here it is. Run it -> Press yellow question mark ( about button ) in the toolbar. CFileDialog(TRUE) will be prompted to you. Enter some file name, which doesn't exist, see how many MessageBox'es will appear.
FileDlgTest.exe
Odd - 3 prompt dialogs.
Which version of visual studio are you using?
Compiled with Visual Studio 2008.
1 min - I'll try with Visual Studio 2010.
with VS2010 -  2 prompt dialogs
Tested on 3 machines, Win7, one of them 64bit, on of them 32bit, third - don't know.
My test was with vs2005 - I'll try a vs2010 app here later this evening.
Ok.tnx
OK, made a small VS2010 app with your code - 2 prompt dialogs appear (as you get).
I've not found a setting to stop that :-(

This sounds like a bug in VS2010 / Win 7

All I can suggest is that you don't have the OFN_CREATEPROMPT flag and test the file exists yourself when the CFileDialog returns.  If the user then says no you could restart the CFileDialog call.
Ok. Anyway, thanks for trying.
PAQ and refund - I could reproduce it here, sounds like a bug.
ASKER CERTIFIED SOLUTION
Avatar of ee_auto
ee_auto

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