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
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;
If the user clicks the little [X] button or presses Alt-F4 or chooses File|Exit or etc., or if you code
CODE
However, if you code
CODE
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