Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check if form is Closed?

Tags:

forms

delphi

vcl

The only way I see is to add flag for this, but is this the best way?

When the form is destroyed and I check if(Assigned(form2)) the result is true? Why?

What is the way to do this?

like image 780
John White Avatar asked Jan 27 '11 10:01

John White


4 Answers

You can use Form1.Showing to see if a form is closed or not.

Just closing a form does not free it unless you set Action := caFree in OnClose event. Default is caHide.

like image 135
Mikael Eriksson Avatar answered Oct 17 '22 14:10

Mikael Eriksson


Wow, a blast from the past :)

The way that Assigned() works, is that it basically does nil check on the pointer. If you destroy form2, there will still be a memory address that form2 points to.

I has been a very long time since I've done any Delphi, but from memory, you need to manually set the form2 var to nil when it is destroyed. If you have a central place (eg. a form broker?) where you create & destroy forms, this should be quite easy.

like image 34
Raymond Barlow Avatar answered Oct 17 '22 12:10

Raymond Barlow


If you use Form1.Free or Form1.Destroy, Delphi will destroy the object but wont set the object reference to nil. So instead use FreeAndNil.

For more information, check Andreas Rejbrand answer in this link

like image 34
Bharat Avatar answered Oct 17 '22 12:10

Bharat


Faced with the same issue when doing some routine on closing application. In this case all forms are destroyed behind the stage but pointers are not set to nil. This code helphs me:

procedure TMyForm.FormDestroy(Sender: TObject);
begin
  MyForm:=nil;
end;

So pointer becomes nil and I can check it with Assigned or compare to nil.

like image 26
Alexander Zaripov Avatar answered Oct 17 '22 13:10

Alexander Zaripov