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?
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
for Form2
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
> 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
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?
> 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