Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Closing Application passing by CloseQuery event

Status
Not open for further replies.

Juhas0

Programmer
May 6, 2008
10
0
0
PL
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?
 
Do you absolutely need the code in the OnClose event?
an alternative would be the move the code to FomDestroy, this means the code will be called when the form is destroyed (even when you do Applciation.Terminate)

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
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.

Hope this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top