Smart questions
Smart answers
Smart people
Join Tek-Tips Forums
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Better method: Auto-create forms or on demand?Helpful Member! 

doctorjellybean (Programmer) (OP)
21 Apr 08 5:49
When one adds forms to the main one, Delphi auto-creates it at runtime. Now I've seen quite a few examples where the form is removed from the Available Forms list and created when needed

Quote:

Form2 := TForm2.Create(nil);
Is there a difference between the two, and what if yes?

Thank you.
Helpful Member!  whosrdaddy (Vendor)
21 Apr 08 7:15
this has already been discussed here:

http://www.tek-tips.com/viewthread.cfm?qid=1438941

lookup my response near the bottom of that thread.

Come back here if it is still not clear.

/Daddy

-----------------------------------------------------
 What You See Is What You Get
Never underestimate tha powah of tha google!

whosrdaddy (Vendor)
21 Apr 08 7:21
to answer your question:

when autocreated, your form eats/memory resources, even if it is not visible/used at that moment.

for an app with 2 forms, this will not make a big difference (depending what's on the form offcourse)
but things could go ugly when your app has a LOT of forms.

I tend to create my objects the moment I need them.
(that's the method you showed)

/Daddy

-----------------------------------------------------
 What You See Is What You Get
Never underestimate tha powah of tha google!

doctorjellybean (Programmer) (OP)
21 Apr 08 8:17

Quote:

whosrdaddy (Vendor)

a)
Formx.Create(Application) -> Form resources will be freed when application is terminated

b)
Formx.Create(Self) -> Form resources are freed when the owner object is destroyed (if Self is a descendand of TComponent)

c)
Formx.Create(nil) -> you are responsible for freeing the form

a is used by delphi when a form is autocreated
b is handy for a main form that has several child windows that need to close when the main form is closed
c is handy for showing a dialog window

I have a normal main menu, and each menu item uses its own form. If I select item 2, form 2 shows, item 3 shows form 3, and so on. When showing e.g. form 3, then form 2 should be closed.

Do I use option (c) Formx.Create(nil) for each form?  
whosrdaddy (Vendor)
21 Apr 08 9:01
yes, that would be the best option.

to prevent the hassle checking which for is in memory at the moment, just create a variable. make sure all forms have an onclose event with the code Action := caFree;

CurrentForm : TForm;

initialize it to nil
CurrentForm := nil;

when clicking on the menu item create your form and associate it with the var

CODE

...

procedure Menu1Click(Sender : TObject);
var Form : TForm1;

begin
// check if we have a form open, if so, close it
// the onclose event will free the form
 if Assigned(CurrentForm) then
  CurrentForm.Close;
// create your new form
 Form1 := TForm1.Create(nil);
 CurrentForm := Form1;
end;

clearer now?

/Daddy

-----------------------------------------------------
 What You See Is What You Get
Never underestimate tha powah of tha google!

sggaunt (Programmer)
21 Apr 08 9:57

Perhaps you shouldn't be doing this, but if you have data stored on/in a form that's not always visible, then creating it at run time isn't an easy option.

To give an example one of my first apps started out with all pre-created forms, later when attempting to convert it to 'create at run time', I found that major re-write would be required to change the way the data was structured.
I could only convert about 2/3 of the forms.

 
 

Steve: N.M.N.F.
Playing the blues isn't about feeling better. It's about making other people feel worse.  

doctorjellybean (Programmer) (OP)
21 Apr 08 11:33

Quote:

whosrdaddy (Vendor)

clearer now?

Yes, I think so. Thank you.

This is the code on my main form.

Quote:

type
  TForm1 = class(TForm)
    MainMenu1: TMainMenu;
    Menu1: TMenuItem;
    Menu2: TMenuItem;
    Menu3: TMenuItem;
    Menu4: TMenuItem;
    Panel1: TPanel;
    procedure Menu1Click(Sender: TObject);
    procedure Menu2Click(Sender: TObject);
    procedure Menu3Click(Sender: TObject);
    procedure Menu4Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  CurrentForm : TForm;

implementation

uses Unit2, Unit3, Unit4;

{$R *.dfm}

procedure TForm1.Menu1Click(Sender: TObject);
var Form : TForm1;
begin
 if Assigned(CurrentForm) then
  CurrentForm.Close;
 Form1 := TForm1.Create(nil);
 CurrentForm := Form1;
end;

procedure TForm1.Menu2Click(Sender: TObject);
var Form : TForm1;
begin
 if Assigned(CurrentForm) then
  CurrentForm.Close;
 Form2 := TForm2.Create(nil);
 CurrentForm := Form2;
 Form2.Show;
end;

procedure TForm1.Menu3Click(Sender: TObject);
var Form : TForm1;
begin
 if Assigned(CurrentForm) then
  CurrentForm.Close;
 Form3 := TForm3.Create(nil);
 CurrentForm := Form3;
 Form3.Show;
end;

procedure TForm1.Menu4Click(Sender: TObject);
var Form : TForm1;
begin
 if Assigned(CurrentForm) then
  CurrentForm.Close;
 Form4 := TForm4.Create(nil);
 CurrentForm := Form4;
 Form4.Show;
end;

I'm sure I'm doing something wrong, as it gives me a message  when compiling:

Quote:

[DCC Warning] Unit1.pas(38): H2164 Variable 'Form' is declared but never used in 'TForm1.Menu1Click'
[DCC Warning] Unit1.pas(47): H2164 Variable 'Form' is declared but never used in 'TForm1.Menu2Click'
[DCC Warning] Unit1.pas(57): H2164 Variable 'Form' is declared but never used in 'TForm1.Menu3Click'
[DCC Warning] Unit1.pas(67): H2164 Variable 'Form' is declared but never used in 'TForm1.Menu4Click'

However, it does appear to work. I think.

I assume I don't need the create routine in the menu1 click procedure, as menu1 is the main form?
whosrdaddy (Vendor)
21 Apr 08 17:01
2 observations:

you don't need the form var in each unit as the form's unit already contains a global variable.

Quote:


I assume I don't need the create routine in the menu1 click procedure, as menu1 is the main form?

yeah, your main form should act as a placeholder for the other forms. so Form1 is autocreated by delphi and the others are controlled by you...

/Daddy

P.S: please use the code tags when posting code

-----------------------------------------------------
 What You See Is What You Get
Never underestimate tha powah of tha google!

doctorjellybean (Programmer) (OP)
21 Apr 08 17:18
Thanks for help and advice!

Oops, I'm so used to using quote tags. Will remember to use code tags smile

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close