Difference between revisions of "Testing, if form exists"

From Free Pascal wiki
Line 1: Line 1:
 
Sometimes a form may be launched from several places in a program. If it already exists, it only needs to be brought to the front. If not, it needs to be created.  
 
Sometimes a form may be launched from several places in a program. If it already exists, it only needs to be brought to the front. If not, it needs to be created.  
  
This method is only needed if the form is not auto created. (It will be listed under Project|Project Options|Forms|Available forms.)
+
This method is only needed if the form is not auto created. (It should be listed under Project|Project Options|Forms|Available forms.)
  
 
The easiest way is:
 
The easiest way is:
  
 
<syntaxhighlight>
 
<syntaxhighlight>
if (MyForm <> nil) then MyForm.Show
+
if (MyForm = nil) then Application.CreateForm(TMyForm, MyForm);
else
+
MyForm.Show;                            
begin
 
  MyForm := TMyForm.Create(Application);
 
  MyForm.Show;  
 
end;                         
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
Use CloseAction := caFree in the form's OnClose event.
 
Use CloseAction := caFree in the form's OnClose event.
 +
 +
<syntaxhighlight>
 +
procedure TMyForm.Formclose(Sender: Tobject; var Closeaction: Tcloseaction);
 +
begin
 +
  CloseAction := caFree;
 +
End;
 +
</syntaxhighlight>
  
 
This method is taken from forum discussions.
 
This method is taken from forum discussions.

Revision as of 21:59, 1 January 2014

Sometimes a form may be launched from several places in a program. If it already exists, it only needs to be brought to the front. If not, it needs to be created.

This method is only needed if the form is not auto created. (It should be listed under Project|Project Options|Forms|Available forms.)

The easiest way is:

if (MyForm = nil) then Application.CreateForm(TMyForm, MyForm);
MyForm.Show;

Use CloseAction := caFree in the form's OnClose event.

 procedure TMyForm.Formclose(Sender: Tobject; var Closeaction: Tcloseaction);
begin
  CloseAction := caFree;
End;

This method is taken from forum discussions.