Solved

Creating forms when needed (500 points)

Posted on 2007-11-19
13
177 Views
Last Modified: 2010-04-05
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.
0
Comment
Question by:rincewind666
  • 4
  • 4
  • 3
  • +1
13 Comments
 
LVL 19

Expert Comment

by:MerijnB
ID: 20311082
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
 
LVL 1

Accepted Solution

by:
tammoz earned 500 total points
ID: 20311523
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
 
LVL 18

Expert Comment

by:Johnjces
ID: 20312555
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
 
LVL 19

Expert Comment

by:MerijnB
ID: 20312626
be aware: you can only use that code when use ShowModal() not Show() !
0
 
LVL 18

Expert Comment

by:Johnjces
ID: 20312643
MerijnB,

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

John
0
 
LVL 19

Expert Comment

by:MerijnB
ID: 20312662
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 18

Expert Comment

by:Johnjces
ID: 20312664
OK. Thanks! Just wondering....
0
 
LVL 19

Expert Comment

by:MerijnB
ID: 20312682
yw,

and please don't be afraid to thoroughly check what other experts say. I so often make silly mistakes... :)
0
 
LVL 18

Expert Comment

by:Johnjces
ID: 20351620
Did any of this help you?

John
0
 

Author Comment

by:rincewind666
ID: 20364290
Sorry.  I've been down for the last few days.  I will get back soon.
0
 

Author Comment

by:rincewind666
ID: 20364496
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
 
LVL 1

Expert Comment

by:tammoz
ID: 20386170
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
 

Author Comment

by:rincewind666
ID: 20616916
Many thanks for your help.  Greatly appreciated.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now