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 Automatically loading a form "on demand" 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
22-May-03
Category
VCL-Forms
Language
Delphi 3.x
Views
136
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Ernesto De Spirito

How can I get my forms created automatically "on demand" (when they are referenced)?

Answer:

If you have programmed in Visual Basic, probably you know what we are talking 
about: when you reference a property or a method of form, it is automatically 
created if necessary. For example, the following code will generate an exception in 
Delphi if Form2 was not previously created: 

Form2.Show;

However it would work perfectly well in Visual Basic (without the semicolon, of 
course), and we can make it work it Delphi too with this little trick: 

1   unit Unit2;
2   
3   interface
4   
5   uses...;
6   
7   type
8     TForm2 = class(TForm)
9       ...
10    end;
11  
12  function Form2: TForm2;
13  
14  var
15    // Form2: TForm2;
16  
17  implementation
18  
19  {$R *.DFM}
20  
21  var
22    RealForm2: TForm2;
23  
24  function Form2: TForm2;
25  begin
26    if RealForm2 <> nil then
27      Form2 := RealForm2
28    else
29      Application.CreateForm(TForm2, Result);
30  end;
31  
32  procedure TForm2.FormCreate(Sender: TObject);
33  begin
34    RealForm2 := Self;
35  end;
36  
37  procedure TForm2.FormDestroy(Sender: TObject);
38  begin
39    RealForm2 := nil;
40  end;
41  
42  initialization
43    RealForm2 := nil;
44  end.


What we did was replacing the Form2 variable with a function with the same name and 
type. This function uses a "hidden" variable (declared in the implementation 
section) -RealForm2- to check if the form is created or not (and in the latter 
case, it will create it automatically). We set the value of this hidden variable in 
the OnCreate and OnDestroy events of the form to the address of the form or nil 
respectively.

Copyright (c) 2001 Ernesto De Spiritomailto:edspirito@latiumsoftware.com
Visit: http://www.latiumsoftware.com/delphi-newsletter.php

			
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