Solved

Creating forms when needed (500 points)

Posted on 2007-11-19
13
181 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

733 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