Articles   Members Online:
-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
Others
Language
Delphi 2.x
Views
12
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  function IsFormCreated(Form: TCustomForm): Boolean;
27  var
28    i: Integer;
29  begin
30    Result := False;
31    for i := 0 to Screen.FormCount - 1 do
32    begin
33      if Screen.Forms[i] = Form then
34      begin
35        Result := True;
36        Break;
37      end;
38    end;
39  end;



Solve 3:

Know whether a form already exist before I dynamically create it

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