Creating forms when needed (500 points)

This question may be simple to many but I am a beginner with Delphi.

When a user clicks a button, I want a form to appear.  At the moment I use Form2.Show, Form3.ShowModal, etc.  However there are a lot of forms and this takes a lot of memory to create all of them when the program is first run.  How can I create these forms just if I need them?

I am using Delphi 6.

As this is urgent, I am giving the maximum 500 points for this.  Thanks.
rincewind666Asked:
Who is Participating?
 
tammozConnect With a Mentor Commented:
hi
when you add a new form to your application (let us say form2), Delphi automatically adds this line to your project
Application.CreateForm(TForm2, Form2);
in order to see this just click ctrl+F12 and choose your project name from the menu and you will see the creation of all your forms

so cut this line and go to your form1 add a button and paste the previous line (Application.CreateForm(TForm2, Form2);) in your button click event and add after it
Form2.Show;
this will show your Form2

then go to your form2 and add this line to the onclose event:
Action := caFree;
this will free the form2 from the memory when you close it so the usage of memory will not increase
if you did not do the last part remember that every time you click your button a new form will be created so you always need to free it when you finish using it

the example code is attached

best regards

1- THE PROJECT
 
program Project1;
 
uses
  Forms,
  Unit1 in 'Unit1.pas' {Form1},
  Unit2 in 'Unit2.pas' {Form2};
 
{$R *.res}
 
begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.
 
2- FORM1
 
unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;
 
type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
 
implementation
 
uses Unit2;
 
{$R *.dfm}
 
procedure TForm1.Button1Click(Sender: TObject);
begin
  Application.CreateForm(TForm2, Form2);
  Form2.Show;
end;
 
end.
 
3- FORM2
 
unit Unit2;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;
 
type
  TForm2 = class(TForm)
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 
var
  Form2: TForm2;
 
implementation
 
{$R *.dfm}
 
procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
end;
 
end.

Open in new window

0
 
MerijnBSr. Software EngineerCommented:
see this example
class blah..
 private
  fForm3: TForm3;
 public
 ...
 end;
 
...
 
procedure TMainForm.Button1Click(Sender: TObject);
begin
 if not assigned(fForm3) then
  fForm3 := TForm3.Create(Self);
 
 fForm3.Show();
end;

Open in new window

0
 
JohnjcesCommented:
This is how I do it...

Since the form is part of your application, here is a way to show the form and then free it when you close the form.

Again, remove the form's create in your .dpr  (project) file but make sure you have designed the form an design time.

In this example, my form name is FAnnivReport.

It will show the form modally, i.e. no other form will gain focus. When this form is finsihed and closed, it is freed.

John
 with TFAnnivReport.Create(Application) do
 try
  ShowModal;
 finally
  Free;
 end;

Open in new window

0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
MerijnBSr. Software EngineerCommented:
be aware: you can only use that code when use ShowModal() not Show() !
0
 
JohnjcesCommented:
MerijnB,

I think I said that! So why did you reiterate it?

John
0
 
MerijnBSr. Software EngineerCommented:
if the topicstarted will use Show, he will probably get an access violation.
I say it just to avoid this curl pit, I didn't (and don't) see any warning on that in your post. ?
0
 
JohnjcesCommented:
OK. Thanks! Just wondering....
0
 
MerijnBSr. Software EngineerCommented:
yw,

and please don't be afraid to thoroughly check what other experts say. I so often make silly mistakes... :)
0
 
JohnjcesCommented:
Did any of this help you?

John
0
 
rincewind666Author Commented:
Sorry.  I've been down for the last few days.  I will get back soon.
0
 
rincewind666Author Commented:
tammoz:

I like your solution BUT, after manually deleting the form from the project, the form disappears and is not available for future editing (and "Action := caFree;" cannot be added to the close event as the form is not available).

Instead, under "Project/Options", I have moved the forms from "auto create" forms to "available forms" and this seems to work. I asume that the memory is still released using "Action := caFree;" on FormClose.  Is this correct?
0
 
tammozCommented:
hi
no you should not delete the form you just delete this line:
Application.CreateForm(TForm2, Form2);
from the project unit and add it to the form1 unit, because you want only to delete the creation operation not the whole form
actually what does the removing from operation do is exactly deleting the creation code automatically rather than the manual method which I explained so you will get the same result

and "Action := caFree" releases the memory

the most important is that you have got what you wanted so congratulations

:)))))))))) best regards
0
 
rincewind666Author Commented:
Many thanks for your help.  Greatly appreciated.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.