I put a TFileSaveDialog
on a form in C++ Builder and set the options fdoOverWritePrompt
, fdoStrictFileTypes
, fdoPathMustExist
, and fdoCreatePrompt
.
When the dialog opens and I select an existing file, it prompts if I want to overwrite like it should. However, if I type in some random name that doesn't exist and click "Save", the dialog just closes - there is no confirmation prompt if it's okay to create one.
Any idea why that is happening?
OFN_CREATEPROMPT
, corresponding to
TFileSaveDialog
's fdoCreatePrompt
orTSaveDialog
's ofCreatePrompt
)is only honored for the Open dialog and never for the Save dialog. Try the same thing with a TFileOpenDialog
and you get that prompt.
Proof for the other dialog types:
var
sd: TSaveDialog;
od: TOpenDialog;
begin
sd:= TSaveDialog.Create( self );
sd.Options:= [ofCreatePrompt]; // Has no effect: no typed-in filename triggers this
sd.Execute();
sd.Free;
od:= TOpenDialog.Create( self );
od.Options:= [ofCreatePrompt]; // When trying to OPEN a not yet existing file
od.Execute();
od.Free;
Why? Logic wise when saving a file in the majority of cases you want to create a new file (select a non-existing filename) anyway - why is there a need to confirm that again when you already have the "Save" button? Saving a file implies that a file is created. Confirming to overwrite/change an existing file is less common.
If you still want such a behavior you have to do it yourself: use the OnCanClose
event and then check the filename being chosen/entered so far:
procedure TForm1.SaveDialog1CanClose(Sender: TObject; var CanClose: Boolean);
begin
if not FileExists( SaveDialog1.FileName ) then begin
// Reject closing the window for every choice but "Yes"
CanClose:= MessageBox( SaveDialog1.Handle, 'Create it?', 'Does not exist yet', MB_YESNO )= IDYES;
end;
end;