Difference between revisions of "Testing, if form exists"

From Free Pascal wiki
Jump to navigationJump to search
m (Fixed syntax highlighting)
 
(10 intermediate revisions by 4 users not shown)
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.
+
{{Testing, if form exists}}
  
This method is only needed if the form is not auto created. (It should be listed under Project|Project Options|Forms|Available forms.)
+
Sometimes a [[TForm|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 [[Form_Tutorial#Create_a_Lazarus_designed_form_dynamically | auto created]] (ie not listed under Project|Project Options|Forms|Available forms).
  
 
The easiest way is:
 
The easiest way is:
  
<syntaxhighlight>
+
<syntaxhighlight lang=pascal>
 
if (MyForm = nil) then Application.CreateForm(TMyForm, MyForm);
 
if (MyForm = nil) then Application.CreateForm(TMyForm, MyForm);
 
MyForm.Show;                             
 
MyForm.Show;                             
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Use CloseAction := caFree in the form's OnClose event.
+
Use <code>CloseAction := caFree</code> in the form's OnClose event, like so:
  
<syntaxhighlight>
+
<syntaxhighlight lang=pascal>
procedure TMyForm.Formclose(Sender: Tobject; var Closeaction: Tcloseaction);
+
procedure TMyForm.FormClose(Sender: Tobject; var Closeaction: Tcloseaction);
 
begin
 
begin
 
   CloseAction := caFree;
 
   CloseAction := caFree;
Line 22: Line 24:
 
This method is taken from forum discussions.
 
This method is taken from forum discussions.
  
[[Category:Code]]
+
{{Warning|This method has some limitations:
[[Category:LCL]]
+
<br/><br/>
[[Category:Forms]]
+
*'''Not''' ''more than one instance'' of that form class should exist at any time.
 +
*Reference to any created form of that class should be stored in ''a single global'' variable.
 +
}}

Latest revision as of 09:10, 29 February 2020

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 (ie not 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, like so:

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:

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