Sign in Join
Wiki copytemporary

Testing, if form exists: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: Testing, if form exists

OldNewLine
1+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.
2+
3+This method is only needed if the form is not [[Form Tutorial|auto created]] (ie not listed under Project|Project Options|Forms|Available forms).
4+
5+The easiest way is:
6+```
7+if (MyForm = nil) then Application.CreateForm(TMyForm, MyForm);
8+ MyForm.Show;
9+```
10+
11+Use `CloseAction := caFree` in the form's OnClose event, like so:
12+```
13+procedure TMyForm.FormClose(Sender: Tobject; var Closeaction: Tcloseaction);
14+ begin
15+ CloseAction := caFree;
16+ MyForm := nil;
17+ End;
18+```
19+
20+This method is taken from forum discussions.
21+
22+[![Warning-icon.png](/wiki-images/b/b2/Warning-icon.png)](</File:Warning-icon.png>)
23+
24+**Warning:** This method has some limitations:
25+
26+
27+ * **Not** _more than one instance_ of that form class should exist at any time.
28+ * Reference to any created form of that class should be stored in _a single global_ variable.
29+
30+---
31+*From the Free Pascal wiki, page "Testing, if form exists", as the Internet Archive kept it on 2024-12-06, last changed on 2020-02-29. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history