Michael
Michael

Reputation: 91

How to prompt user to confirm program exit?

I'm trying to do what my question says, when the user clicks the close button on the form, I want to prompt the user to confirm it, this is the code I came up with... It is problematic, when I click on the close button, it does prompt me to confirm, but the program closes anyway, irrelevant of the button I click, what am I doing wrong?

procedure TfrmLogin.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  if MessageDlg('Are you sure you want to exit?', mtWarning, [tMsgDlgBtn.mbYes, tMsgDlgBtn.mbCancel], 0) = 0 then
    begin
      Application.Terminate;
    end;
end;

Upvotes: 0

Views: 656

Answers (1)

Andreas Rejbrand
Andreas Rejbrand

Reputation: 108948

When you read the documentation for TForm.OnClose, you must have missed the table telling you what you can put in the Action parameter:

The TCloseEvent type points to a method that handles the closing of a form. The value of the Action parameter determines if the form actually closes. These are the possible values of Action:

caNone

The form is not allowed to close, so nothing happens.

caHide

The form is not closed, but just hidden. Your application can still access a hidden form.

caFree

The form is closed and all allocated memory for the form is freed.

caMinimize

The form is minimized, rather than closed. This is the default action for MDI child forms.

Hence, you can do

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  if MessageBox(Handle, 'Do you want to close this app?', 'Super App', MB_YESNOCANCEL) = ID_YES then
    Action := caHide
  else
    Action := caNone;
end;

But it is arguably better to use the OnCloseQuery event:

procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  CanClose := MessageBox(Handle, 'Do you want to close this app?', 'Super App', MB_YESNOCANCEL) = ID_YES;
end;

Upvotes: 2

Related Questions