Link to home
Start Free TrialLog in
Avatar of rincewind666
rincewind666

asked on

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.
Avatar of MerijnB
MerijnB
Flag of Netherlands image

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

ASKER CERTIFIED SOLUTION
Avatar of tammoz
tammoz

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

be aware: you can only use that code when use ShowModal() not Show() !
MerijnB,

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

John
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. ?
OK. Thanks! Just wondering....
yw,

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

John
Avatar of rincewind666
rincewind666

ASKER

Sorry.  I've been down for the last few days.  I will get back soon.
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?
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
Many thanks for your help.  Greatly appreciated.