Difference between revisions of "Testing, if form exists"

From Free Pascal wiki
(I think you meant forum discussions ;) Thanks for the page!)
m (Fixed syntax highlighting)
 
(17 intermediate revisions by 7 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}}
  
One way to do this is to use a global boolean variable which is set to true or false as the form is created or destroyed.
+
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.  
  
Another way is to use a function as below.
+
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).
  
These functions are copied from forum discussions.
+
The easiest way is:
  
How to list all data modules opened in an Application? [http://forum.lazarus.freepascal.org/index.php/topic,20141.msg115801.html#msg115801]
+
<syntaxhighlight lang=pascal>
 +
if (MyForm = nil) then Application.CreateForm(TMyForm, MyForm);
 +
MyForm.Show;                           
 +
</syntaxhighlight>
 +
 
 +
Use <code>CloseAction := caFree</code> in the form's OnClose event, like so:
  
<syntaxhighlight>
+
<syntaxhighlight lang=pascal>
function FindForm(const aClass:TClass):TForm;
+
procedure TMyForm.FormClose(Sender: Tobject; var Closeaction: Tcloseaction);
var
 
  I: Integer;
 
begin
 
  for I := 0 to Screen.FormCount -1 do
 
    if Screen.Forms[vCnt].ClassType = aClass then begin
 
      Result := Screen.Forms[I];
 
      Break;
 
    end;
 
end;
 
</syntaxhighlight>
 
Call it with:
 
<syntaxhighlight>
 
if FindForm(TMyForm) <> nil then MyForm.Show
 
else
 
 
begin
 
begin
   MyForm := TMyForm.Create(Application);
+
   CloseAction := caFree;
   MyForm.Show;  
+
   MyForm := nil;
end;                          
+
End;
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Use CloseACtion := caFree in the form onClose event.
+
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 10: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.