2
I am using a code to ask the user if he wants to save the changes before leaving the program (Yes, No and Cancel buttons).
The problem is that when the user clicks to save before exiting (Yes button) if he closes the Savedialog that was opened, the program is finished without saving anything (the correct one would be the program not finish, because the user has given up saving).
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;
Procedure for saving document:
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?
Ask the question the function code
SaveDocument
– Roberto de Campos