Difference between revisions of "Testing, if form exists"

From Free Pascal wiki
Jump to navigationJump to search
Line 23: Line 23:
  
 
This method is taken from forum discussions.
 
This method is taken from forum discussions.
 +
 +
{{Warning|This method has some limitations:
 +
<br/><br/>
 +
*At any time should not exist more than one instance of that form class.
 +
*Reference to any created form of that class should be stored at single global variable.
 +
}}
 +
  
 
[[Category:Code]]
 
[[Category:Code]]
 
[[Category:LCL]]
 
[[Category:LCL]]
 
[[Category:Forms]]
 
[[Category:Forms]]

Revision as of 21:04, 3 May 2015

Deutsch (de) English (en) français (fr)

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;
  MyForm := nil;
End;

This method is taken from forum discussions.

Warning-icon.png

Warning: This method has some limitations:

  • At any time should not exist more than one instance of that form class.
  • Reference to any created form of that class should be stored at single global variable.