How to use MessageDlg in OnClose Event of Delphi Form?

delphi, delphi-xe2, dialog

Solution

procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
var
  buttonSelected: integer;
begin
  buttonSelected := MessageDlg('Do you really want to close the application?', mtCustom, [mbYes, mbNo], 0);
  if buttonSelected = mrYES then
  begin
    CanClose:=true;
  end
  else
  begin
    CanClose:=false;
  end;
end;

or as @TLama advised, to simplify:

procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  CanClose := MessageDlg('Do you really want to close the application?', mtCustom, [mbYes, mbNo], 0) = mrYES;
end;

Problem

When my form is closed by clicking on 'Cross Button' or Alt + F4, I want user to ask whether he wants to close the application or not. If yes, I will terminate the application otherwise nothing to do. I am using following code on the onclose event of the form ``` procedure MyForm.FormClose(Sender: TObject; var Action: TCloseAction); var buttonSelected : integer; begin buttonSelected := MessageDlg('Do you really want to close the application?',mtCustom, [mbYes,mbNo], 0); if buttonSelected = mrYES then begin Application.Terminate; end else begin //What should I write here to resume the application end; end; ``` Whether I click Yes or No, my application is terminating. What should I do so that on No click of confirmation box, my application should not terminate. How should I improve my above function? Am I using right form event to implement this functionality? Please help..

Original source