Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Application.CreateForm vs. TForm.Create

4,697 views
Skip to first unread message

Howard Moon

unread,
Jul 19, 2000, 3:00:00 AM7/19/00
to
Hi...
just out of curiosity, what's the difference between calling TForm.Create
and Application.CreateForm? The project file obviously uses CreateForm for
auto-created forms, but we've been using TMyForm.Create to launch forms of
our own from code. Even the Help example of CreateForm includes the line
"with TForm5.Create(nil) do". When should we use FormCreate instead of
TMyForm.Create (if ever)?
-Howard


Alexandre Eduardo ( BR )

unread,
Jul 19, 2000, 3:00:00 AM7/19/00
to
Hi

Theres a basic one.

The App.CreateForm is a application´s method and all forms created with this
are owned by app. But if you need for example just create a form with no
owner like Create(Self) you have to use the form´s create.
Another behavior that I finded out is if you use the app.createform and you
create 2 instances of the same form, the app create for the second one just
a pointer for the first and if you destroy for example the second one, the
first is destroyed too. I may cause exception in your app.


I´m not sure if I was clear but I try!!
Alexandre Eduardo BR


Howard Moon escreveu na mensagem <8l42ek$ji...@bornews.borland.com>...

Mike Orriss (TeamB)

unread,
Jul 19, 2000, 3:00:00 AM7/19/00
to
In article <8l42ek$ji...@bornews.borland.com>, Howard Moon wrote:
> just out of curiosity, what's the difference between calling TForm.Create
> and Application.CreateForm?
>

The latter sets the MainForm attribute for the first form created. There is
never a need to use it outside the DPR file.


Mike Orriss (TeamB & Developer Express)
(Unless stated otherwise, my replies relate to Delphi 5)
(No unsolicited e-mail replies please)


Craig Stuntz

unread,
Jul 19, 2000, 3:00:00 AM7/19/00
to

"Alexandre Eduardo ( BR )" wrote:
>
> The App.CreateForm is a application´s method and all forms created with this
> are owned by app. But if you need for example just create a form with no
> owner like Create(Self) you have to use the form´s create.

Application.CreateForm will also make the form created the
applicaiton's main form if a main form does not already exist.

I use TForm.Create when I'm creating forms myself, and let Delphi use
Application.CreateForm for autocreated forms, which, in my apps, are the
main form only.

-Craig

--
Craig Stuntz Vertex Systems Corporation
Senior Developer http://www.vertexsoftware.com

Jonas Malmsten

unread,
Jul 19, 2000, 3:00:00 AM7/19/00
to
There is on more difference. CreateForm sets the global form variable
before the form is created, ie

Form1 := TForm1.Create(Application);

will crash if you for instance refer to variable Form1 in the forms
OnCreate method,

Application.CreateForm(TForm1, Form1)

will not crash in such a case. However, you shouldn't need to refer to
the global declaration Form1, so if your code is well written (the way
I see it) use Form1 := TForm1.Create(Application {or watever owner you
want}) instead.

//Jonas


In article <8l42ek$ji...@bornews.borland.com>,
"Howard Moon" <hm...@landstar.com> wrote:
> Hi...


> just out of curiosity, what's the difference between calling
TForm.Create

> and Application.CreateForm? The project file obviously uses
CreateForm for
> auto-created forms, but we've been using TMyForm.Create to launch
forms of
> our own from code. Even the Help example of CreateForm includes the
line
> "with TForm5.Create(nil) do". When should we use FormCreate instead
of
> TMyForm.Create (if ever)?
> -Howard
>
>


Sent via Deja.com http://www.deja.com/
Before you buy.

John Pierce

unread,
Jul 19, 2000, 3:00:00 AM7/19/00
to
One other small point: with TForm.Create(nil) do does not specify an
owner for the form, so you must free it. If in Form1 you write: with
TForm.Create(Self) do, then Form1 is the owner of the form and will free
it upon being freed itself. This doesn't mean you shouldn't free the
form, and I do so routinely.

John Pierce


Howard Moon

unread,
Jul 19, 2000, 3:00:00 AM7/19/00
to
Thanks, all...
-Howard


Alexandre Eduardo ( BR )

unread,
Jul 19, 2000, 3:00:00 AM7/19/00
to
That´s it!! It´s a good practice.


Craig Stuntz escreveu na mensagem
<3975B681.8F8987B@no_spam.vertexsoftware.com>...

Mike Williams

unread,
Jul 19, 2000, 3:00:00 AM7/19/00
to
> During runtime created forms should IMHO always contain a caFree in
> their OnClose method.

Only if you show the form non-modally. For modal forms you typically
create, showmodal, and free them all in one sequence.

-Mike

Mike Williams

unread,
Jul 19, 2000, 3:00:00 AM7/19/00
to
> But where should be the advantage of this solution?

None if you're just using the modal result. However, I typically create the
form, populate it with data, show it modally, do something with the edited
data, and then free the form.

-Mike

Alexander Mueller

unread,
Jul 20, 2000, 3:00:00 AM7/20/00
to

During runtime created forms should IMHO always contain a caFree in
their OnClose method.

Alex
--
*** http://www.bullterrier-in-not.de/massenmord.htm ***

Author of the free Chatsystem PINO! || Website: http://www.alcomp.net
Available at http://pino.cjb.net || Chat : pinochat.dhs.org:8080

Alexander Mueller

unread,
Jul 20, 2000, 3:00:00 AM7/20/00
to
Mike Williams wrote:
>
> Only if you show the form non-modally. For modal forms you typically
> create, showmodal, and free them all in one sequence.
>
> -Mike

But where should be the advantage of this solution?

Alex

Alexander Mueller

unread,
Jul 20, 2000, 3:00:00 AM7/20/00
to
Mike Williams wrote:
>
> None if you're just using the modal result. However, I typically create the
> form, populate it with data, show it modally, do something with the edited
> data, and then free the form.
>
> -Mike

OK, for such a usage you have to free it manually. Normally I just think
its better to centralize the code.

Craig Stuntz

unread,
Jul 20, 2000, 3:00:00 AM7/20/00
to

Alexander Mueller wrote:
>
> But where should be the advantage of this solution?

It's a good practice to free memory in the same place it was allocated,
whenever possible.

0 new messages