I am using a code to ask the user if he wants to save the changes before leaving the program (Yes, No, Cancel).
The problem is that when the user clicks to save before exiting (Yes button) if it closes the SaveDialog that was opened, the program is terminated without saving anything (the correct one would be the program did not finish because the user gave up save).I'm using the following code:
private
{ Private declarations }
FFileName: string;
resourcestring
sSaveChanges = 'Salvar alterações de %s?';
procedure TFrmMain.CheckFileSave;
var
SaveResp: Integer;
begin
if REdtMinhaLista.Modified = false then Exit; //se o RichEdit for modificado
SaveResp := MessageDlg(Format(sSaveChanges, [FFileName]),
mtConfirmation, mbYesNoCancel, 0);
case SaveResp of
idYes: SaveDocument; //problema: se o usuário clicar "Sim" e fechar o SaveDialog o programa fecha.
idNo: ; //Nothing
idCancel: Abort;
end;
end;
procedure TFrmMain.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
try
CheckFileSave;
except
CanClose := False;
end;
end;
Document save procedure:
procedure TFrmMain.SaveDocument;
begin
if FFileName = sUntitled then
SaveAsDocument
else
begin
REdtMinhaLista.Lines.SaveToFile(FFileName);
REdtMinhaLista.Modified := False;
SetModified(False);
end;
end;
How could I solve this problem?