Difference between revisions of "Testing, if form exists"

From Lazarus wiki
Jump to navigationJump to search
Line 12: Line 12:
Use CloseAction := caFree in the form's OnClose event.
Use CloseAction := caFree in the form's OnClose event, like so:

Revision as of 15:51, 31 October 2017

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);

Use CloseAction := caFree in the form's OnClose event, like so:

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

This method is taken from forum discussions.


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.