Sunday, January 9, 2011

Closing Application passing by CloseQuery event

Q :
Hi, I've got an unit which will be added to other my applications. And I've got a problem.

In certain moment I must close application, but I have to be sure that code included in OnClose event will be executed.
Another problem is that some applications will have OnCloseQuery event, and some not.

As far as I know, I can't use Application.Terminate nor Halt, because code in OnClose will not be executed.

Application.MainForm.Close; can't be used as well, because if there is OnCloseQuery event, application will not close.

So, how to close application and execute code in OnClose(if there is any)?

Maybe something with compiler directives? But what?

The whole purpose of OnCloseQuery is to let your application decide whether or not it closes. If you always indicate "no" then the only way is to go through Application.Terminate or Halt.

If you want to have a way of terminating your application immediately without the user's permission, you might use a simple boolean flag:


  TMainForm = class(TForm)
    procedure FormCloseQuery(Sender: TObject; var CanClose: boolean);
    FIsCloseNow: boolean;
    procedure CloseNow;

procedure TMainForm.FormCloseQuery(Sender: TObject; var CanClose: boolean);
  if FIsCloseNow
    then CanClose := true
    else CanClose := MessageDlg(
                       'Do you really want to quit?',
                       [mbYes, mbNo],
                   = mrYes

procedure TMainForm.CloseNow;
  FIsCloseNow := true;
Using the FIsCloseNow flag (which is auto-initialized to false by the object construction process) you can control how the application terminates.

If the user clicks the little [X] button or presses Alt-F4 or chooses File|Exit or etc., or if you code


then the user will be asked if he really wants to quit.

However, if you code


then the user is not asked if he really wants to quit before the application terminates out from under his cursor.

Remember, good UI means your application should do what the user thinks it will do, so this kind of dual behavior is best used carfully.

Twitter Delicious Facebook Digg Stumbleupon Favorites More

Design by Kang Iwan K-sev | Thank's for your visit To My Site - Ridwan Mulyana | Cibeureum