Articles   Members Online: 3
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to check if a TForm has already been created Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
30-Aug-02
Category
VCL-Forms
Language
Delphi 2.x
Views
22
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Tomas Rutkauskas

How to check if a TForm has already been created

Answer:

Solve 1:

You just need to provide a little code to manage the lifetime of the form. 
Something like this (which assumes there should only ever be one instance of the 
TForm1 class):

1   interface
2   
3   procedure ShowForm;
4   procedure CloseForm;
5   
6   implementation
7   
8   var
9     Form: TForm1;
10  
11  procedure ShowForm;
12  begin
13    if (Form <> nil) then
14      Form.Show
15    else
16    begin
17      Form := TForm1.Create(Application);
18      Form.Show;
19    end;
20  end;


You can only check for nil the first time, because once you create and free the 
form it won't be nil again unless you make it nil in code!

21  procedure CloseForm;
22  begin
23    Form.Free;
24    Form := nil;
25  end;


Then simply call these help functions whenever you want to show or close the form.


Solve 2:

You can use the FindWindow API function, but for checking if a form has been 
created within an application, this is easier:
26  
27  function IsFormCreated(Form: TCustomForm): Boolean;
28  var
29    i: Integer;
30  begin
31    Result := False;
32    for i := 0 to Screen.FormCount - 1 do
33    begin
34      if Screen.Forms[i] = Form then
35      begin
36        Result := True;
37        Break;
38      end;
39    end;
40  end;



Solve 3:

Know whether a form already exist before I dynamically create it
41  
42  function IsFormOpen(const FormName: string): Boolean;
43  var
44    i: Integer;
45  begin
46    Result := False;
47    for i := Screen.FormCount - 1 downto 0 do
48      if (Screen.Forms[i].Name = FormName) then
49      begin
50        Result := True;
51        Break;
52      end;
53  end;
54  
55  //First check, if the Form (here Form2) is open. If not, create it. 
56  
57  procedure TForm1.Button1Click(Sender: TObject);
58  begin
59    if not IsFormOpen('Form2') then
60      Form2 := TForm2.Create(Self);
61  
62    Form2.Show
63  end;
64  
65  { For MDI Children }
66  
67  function IsMDIChildOpen(const AFormName: TForm; const AMDIChildName: string): 
68  Boolean;
69  var
70    i: Integer;
71  begin
72    Result := False;
73    for i := Pred(AFormName.MDIChildCount) downto 0 do
74      if (AFormName.MDIChildren[i].Name = AMDIChildName) then
75      begin
76        Result := True;
77        Break;
78      end;
79  end;
80  
81  //First check, if the MDI Child is open. If not, create it. 
82  
83  procedure TForm1.Button2Click(Sender: TObject);
84  begin
85    if not IsMDIChildOpen(Form1, 'MyMDIChild') then
86      MyMDIChild := TMyMDIChild.Create(Self);
87    MyMDIChild.Show;
88    MyMDIChild.BringToFront;
89  end;





			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC