Difference between revisions of "Testing, if form exists"

From Lazarus wiki
Jump to navigationJump to search
(Test that form exists, Find form)
 
m (Fixed syntax highlighting)
 
(19 intermediate revisions by 8 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 Wiki 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>
  
<syntaxhighlight>
+
Use <code>CloseAction := caFree</code> in the form's OnClose event, like so:
function FindForm(const aClass:TClass):TForm;
+
 
var
+
<syntaxhighlight lang=pascal>
  I: Integer;
+
procedure TMyForm.FormClose(Sender: Tobject; var Closeaction: Tcloseaction);
 
begin
 
begin
   for I := 0 to Screen.FormCount -1 do
+
   CloseAction := caFree;
    if Screen.Forms[vCnt].ClassType = aClass then begin
+
   MyForm := nil;
      Result := Screen.Forms[I];
+
End;
      Break;
 
    end;
 
end;
 
</syntaxhighlight>
 
Call it with:
 
<syntaxhighlight>
 
if FindForm(TMyForm) <> nil then MyForm.Show
 
else
 
begin
 
   MyForm := TMyForm.Create(Application);
 
  MyForm.Show;  
 
end;                          
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Use CloseACtion := caFree in the form onClose event.
+
This method is taken from forum discussions.
 +
 
 +
{{Warning|This method has some limitations:
 +
<br/><br/>
 +
*'''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.