Abort Program Exit Procedure

Asked

Viewed 217 times

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?

  • 1

    Ask the question the function code SaveDocument

1 answer

1


The only thing you need to do is change the values of the no case leave the example:

case SaveResp of
  6: SaveDocument; //idYes
  7: ; //Nothing   //idNo
  2: Abort;        //idCancel
end;

Complete list of values matching:

mrYes      = 6
mrNo       = 7
mrOK       = 1
mrCancel   = 2
mrAbort    = 3
mrRetry    = 4
mrIgnore   = 5
mrAll      = 8
mrNoToAll  = 9
mrYesToAll = 10

More information can be found here.

EDIT1:

I made a simple example of using the message to create a folder in a dir, I leave an example:

//Declaro VClose como variavel global para fechar ou não o projecto 
var
  Form1: TForm1;
    VClose: Boolean;

procedure TForm1.FormCreate(Sender: TObject);
begin
  //passo a variavel para falso para não fechar a aplicação enquanto não der permição
  VClose := False;
end;

procedure TForm1.CheckFileSave;
var SaveResp: Integer;
    sSaveChanges, FFileName: String;
begin
  sSaveChanges := 'Salvar alterações de %s?';
  FFileName := 'FileName';
  SaveResp := MessageDlg(Format(sSaveChanges, [FFileName]), mtConfirmation, mbYesNoCancel, 0);

  //usei um memo para ter a certeza que está tudo a passar no sitio correto
  if SaveResp = mrYes then
    Begin
      Memo1.Lines.Add('Yes pressed');
      SaveDocument; //vamos criar a pasta com falei acima
    End
  else if SaveResp = mrNo then
    Begin
      Memo1.Lines.Add('No pressed');
      VClose := True;  //se clicou não gravar então passo a var a true
    End
  else if SaveResp = mrCancel then
    Begin
      Memo1.Lines.Add('Cancel pressed');  //não fazemos nada
    End;
end;

procedure TForm1.SaveDocument; 
var NameDir: String;
begin
  NameDir := 'C:\TestDir';   //atribui o caminho
  if ForceDirectories(NameDir) then
    Begin
      memo1.Lines.add('Folder Created');
      VClose := True;  //se criou passo a var a true
    End
  else memo1.Lines.add('New directory failed with error : '+ IntToStr(GetLastError));
end;

procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  CheckFileSave;

  CanClose := VClose; //só fecha de VClose for igual a true;
end;

I tried to drill down on the code but any questions let me know, the only thing you need to do is change the code of savedocument for how you need it. But first I would advise you to test the code to make sure it works the way you want it to.

The "secret" for things to work well is in the variable VClose, attributing false there is variable it will never close the project.

  • @lukkicode needs you to clarify something else?

  • It’s still not working. The problem is when the user clicks on "Yes" in the dialog box, Savedialog opens and the user closes that same Savedialog. Then the program goes off without saving anything. Try to open Ms Word type something, try to quit, click yes and close the save dialog box. The word is still open.

  • @lukkicode managed to help him?

  • Perfect, thank you very much. I ended up finding a demo of a component (Trichview) that also has a code similar to yours, but with the use of functions instead of procedures.

  • @Tmc, I would change the CreateDir for ForceDirectories :)

  • @Júniormoreira good tip, as it is just an example and not what was questioned in the question, I used what came to me faster

  • 1

    @Tmc, understood! The advantage of it is that you do not need to test, because if it does not exist it already creates!

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.