Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Bizarre Form Close Behavior

87 views
Skip to first unread message

Sue D. Nom

unread,
Oct 11, 2001, 9:35:04 PM10/11/01
to
Running D5, patch 1

Create a Form1.
Create a Form2.

On Form1, drop 3 buttons

Form1Show, Form2Close, Form1Close;

Add the following code to the OnClick event for each button.

procedure TForm1.Form2ShowClick(Sender: TObject);
begin
Form2.Show;
end;

procedure TForm1.Form2CloseClick(Sender: TObject);
begin
Form2.Close;
end;

procedure TForm1.Form1CloseClick(Sender: TObject);
begin
Form1.Close;
end;

Lastly, in Form2, add code to the OnClose and OnDestroy events of the
form

procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
MessageDlg('In OnClose Event - Form2',mtInformation,[mbOK],0);
end;

procedure TForm2.FormDestroy(Sender: TObject);
begin
MessageDlg('In OnDestroy Event - Form2',mtInformation,[mbOK],0);
end;

Now, run the program. You will find that whenever you click the 'Close
Form2 Button', you will always get the close event message. But, if you
close form1, you will get neither message.

So... add a procedure to Form2.

procedure TForm2.ShowAMessage;
begin
ShowMessage('What the Hey???');
end;

Now, in Form1.OnClose call the Form2 procedure

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Form2.ShowAMessage;
end;

Whaddaya know? It executes.

This seems odd to me. It means that if I wish to perform any
'pre-close' operations on junior forms, I must execute them out of the
main form, as opposed to letting them fire in the OnClose events of the
form in which they exist. What's also interesting is that the Form2
OnDestroy event is never called. Am I missing something?

Sue D. Nom

unread,
Oct 11, 2001, 9:48:46 PM10/11/01
to
Allow me to slightlu redefine the question. When Form1 gets closed, Form2
ends up being closed but its event handler does not fire. Why not?

Igor Ivanov

unread,
Oct 12, 2001, 2:25:30 AM10/12/01
to
OnDestroy fires in any case. Put a stop in its handler and you'll see. When
you close Form1, the app is being terminated and apparently Delphi is not
able to show any forms any longer (something like the message loop needed
for the dialog to function is already not working - not sure about details),
MessageDlg forms included. So MessageDlg executes without making anything
visible and returns. You need to use Win API MessageBox to show messages in
this case.

As documented, OnClose only fires in response to Close or when a form is
closed by the user. It doesn't fire when you just destroy a form, which is
the case when you terminate the app by closing its main form (Form1).

Igor


Igor Ivanov

unread,
Oct 12, 2001, 2:32:31 AM10/12/01
to

"Igor Ivanov" <igivanovR...@infoserve.net> wrote in message
news:3bc68bc9_2@dnews...
...

> As documented, OnClose only fires in response to Close or when a form is
> closed by the user. It doesn't fire when you just destroy a form, which is
> the case

for Form2

Sue D. Nom

unread,
Oct 12, 2001, 9:32:05 AM10/12/01
to
Right you are. Unfortunately, the behavior with respect to NOT executing a
MessageDlg is not very nice. For example, if a user has a file that has changed
and has not been saved, it would be nice to output a message asking if the file
should be saved. Now I know that while I can close the file in an OnDestroy
event, I cannot issue a MessageDlg there.

Raghavendra Rao

unread,
Oct 12, 2001, 11:21:37 AM10/12/01
to

"Sue D. Nom" <su...@courthouse.com> wrote in message
news:3BC6F0D5...@courthouse.com...

> Right you are. Unfortunately, the behavior with respect to NOT executing
a
> MessageDlg is not very nice. For example, if a user has a file that has
changed
> and has not been saved, it would be nice to output a message asking if the
file
> should be saved. Now I know that while I can close the file in an
OnDestroy
> event, I cannot issue a MessageDlg there.

What about the CanCLoseQuery ?
If that too does not work, use a flag in the second form which will be set
when it is saved
Check that in the onclose of the main form.

HTH
Raghu


Iman L Crawford

unread,
Oct 12, 2001, 11:13:55 AM10/12/01
to
"Sue D. Nom" <su...@courthouse.com> wrote in
news:3BC6F0D5...@courthouse.com:

> Right you are. Unfortunately, the behavior with respect to NOT
> executing a MessageDlg is not very nice. For example, if a user has a
> file that has changed and has not been saved, it would be nice to
> output a message asking if the file should be saved. Now I know that
> while I can close the file in an OnDestroy event, I cannot issue a
> MessageDlg there.

Try the OnCloseQuery event to determine if a form should close, also the
OnCloseQuery event of TForm1 should check something in TForm2 to determine
if it should close.


--
Iman
`There are reminders to all Americans that they need to watch what they
say, watch what they do, and this is not a time for remarks like that;
there never is.' - Ari Fleischer

Sue D. Nom

unread,
Oct 12, 2001, 12:21:12 PM10/12/01
to
OnCloseQuery is also not called when the mainform is closed.

Iman L Crawford wrote:

> Try the OnCloseQuery event to determine if a form should close, also the
> OnCloseQuery event of TForm1 should check something in TForm2 to determine
> if it should close.
>

PS: Iman - What was Ari Fleischer referring to in your comment below?

Iman L Crawford

unread,
Oct 12, 2001, 1:06:57 PM10/12/01
to
"Sue D. Nom" <su...@courthouse.com> wrote in
news:3BC71878...@courthouse.com:

> OnCloseQuery is also not called when the mainform is closed.

In your OnCloseQuery of TForm1(mainform) check the other forms to see if
they need to save first, then call a save method of the forms before
exiting the OnCloseQuery event.

> PS: Iman - What was Ari Fleischer referring to in your comment below?

This came about after the guy from politically incorrect said some stupid
stuff on tv. The wording Ari used really bugged me, so I put it in my sig
as a reminder of what we are fighting for.

--
Iman

0 new messages