Saturday, January 8, 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?

A:
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:

CODE

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

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

procedure TMainForm.CloseNow;
  begin
  FIsCloseNow := true;
  Close
  end;
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

CODE

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

However, if you code

CODE

CloseNow;
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.

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

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