Board index » delphi » onCreate vs Loaded for Forms

onCreate vs Loaded for Forms

When setting up forms, what tasks are appropriate and inappropriate for
inclusion in TForm1.Create and TForm1.Loaded. I'm particularly interested in
dynamic creation of components, eg., adding a number of buttons to a panel.

TIA
--
Bill Boulton
using D3 Std on a 98SE box

 

Re:onCreate vs Loaded for Forms


Quote
In article <3d721d6c.248...@forums.borland.com>, Billboulton wrote:
> When setting up forms, what tasks are appropriate and inappropriate for
> inclusion in TForm1.Create and TForm1.Loaded. I'm particularly interested in
> dynamic creation of components, eg., adding a number of buttons to a panel.

I would do that in OnCreate. If you override the constructor, call the
inherited constructor first, then add your controls. Loaded actually executes
before OnCreate, but it has the drawback that it may be called more than once
if your form uses visual form inheritance. Both Loaded and OnCreate are
executed "in" the inherited Create if you override the constructor.

--
Peter Below (TeamB)  
Use the newsgroup archives :
http://www.mers.com/searchsite.html
http://www.tamaracka.com/search.htm
http://groups.google.com
http://www.prolix.be

Re:onCreate vs Loaded for Forms


Why call the override and call the inherited constructor. I would usually
declare the component as a private member of the form, then create it in the
create constructor as is, e.g.

procedure TFotrm1.FormCreate(Sender:TObject);
Begin
    With Button1 do
    Begin
        Create(Self);
        Parent:=Self;
        Left:=0;
        .
        .
    End;
End;

"Peter Below (TeamB)" <100113.1...@compuXXserve.com> wrote in message
news:VA.000091ea.00158fc5@antispam.compuserve.com...

Quote
> In article <3d721d6c.248...@forums.borland.com>, Billboulton wrote:
> > When setting up forms, what tasks are appropriate and inappropriate for
> > inclusion in TForm1.Create and TForm1.Loaded. I'm particularly
interested in
> > dynamic creation of components, eg., adding a number of buttons to a
panel.

> I would do that in OnCreate. If you override the constructor, call the
> inherited constructor first, then add your controls. Loaded actually
executes
> before OnCreate, but it has the drawback that it may be called more than
once
> if your form uses visual form inheritance. Both Loaded and OnCreate are
> executed "in" the inherited Create if you override the constructor.

> --
> Peter Below (TeamB)
> Use the newsgroup archives :
> http://www.mers.com/searchsite.html
> http://www.tamaracka.com/search.htm
> http://groups.google.com
> http://www.prolix.be

Re:onCreate vs Loaded for Forms


Quote
In article <3d738843.625...@forums.borland.com>, Billboulton wrote:
> Peter, given a setup as you describe, would I be right in saying that a
> form's onCreate event handler is the last task performed as part of
> Application.CreateForm for that particular form?

Yes.

--
Peter Below (TeamB)  
Use the newsgroup archives :
http://www.mers.com/searchsite.html
http://www.tamaracka.com/search.htm
http://groups.google.com
http://www.prolix.be

Re:onCreate vs Loaded for Forms


Quote
>inherited constructor first, then add your controls. Loaded actually executes
>before OnCreate, but it has the drawback that it may be called more than once

Really?

But should we call inherited in Loaded anyway? Can I see somehow, if
and when Loaded is called more than once?

cu Christian

Re:onCreate vs Loaded for Forms


Quote
In article <=KF1PR9Jk41+bsBNtGlfE43HR...@4ax.com>, Christian Kaufmann wrote:
> >inherited constructor first, then add your controls. Loaded actually executes
> >before OnCreate, but it has the drawback that it may be called more than once
> Really?

Yup, really. It is a "feature" of visual form inheritance, were each ancestor
forms DFM gets loaded in turn.

Quote

> But should we call inherited in Loaded anyway?

Yes.

Quote
> Can I see somehow, if and when Loaded is called more than once?

If this is critical for you keep track of the calls using a private flag in your
form class. If FAlreadyLoaded Then Exit; ....

--
Peter Below (TeamB)  
Use the newsgroup archives :
http://www.mers.com/searchsite.html
http://www.tamaracka.com/search.htm
http://groups.google.com
http://www.prolix.be

Re:onCreate vs Loaded for Forms


Quote
In article <3d776fc0.3448...@forums.borland.com>, Billboulton wrote:
> So now seems like the right time to read the registry/ini file and restore
> the component state, eg., size, position.

Yes, i usually do this kind of initialization in OnCreate and the saving in
OnCloseQuery.

Quote
> But, with certainty,  _only_ for that particular component which is known to
> have been created?

You can always test that, the field for the component will be nil before the
component has been loaded.

Quote
> Now all we need is an auto-create thingy which can perform any remaining
> initialization processing which requires a fully formed and populated
> application environment. I mean without hacking the .dpr file, although that
> is the most obvious solution. Can Delphi be relied upon to restrict its'
> auto editing to the uses list and the Application.CreateForm() statements?

Yes.

Quote
> How safe would it be to use PostMessage in the overridden constructor of the
> last (or any other if the destination handle was available) form created?

Pretty safe. Doing so will force the forms window handle to be created,
though. I usually use such a message to trigger processing that should only
start after the form has been shown.

Quote
> My
> thinking would be that the message handler would among the first (if not the
> first) tasks performed by the now running application. What I can't work out
> is when is the handle for a form created and when does the Application
> object get a viable handle?

The Application object already has a valid handle before the DPR files main
block is entered. A forms handle will be created when code first refers to it,
or when the form is first shown, whatever happens first.

--
Peter Below (TeamB)  
Use the newsgroup archives :
http://www.mers.com/searchsite.html
http://www.tamaracka.com/search.htm
http://groups.google.com
http://www.prolix.be

Re:onCreate vs Loaded for Forms


On Thu, 05 Sep 2002 20:41:50 +0200, "Peter Below (TeamB)"

Quote
<100113.1...@compuXXserve.com> wrote:

[snip]
Quote
>The Application object already has a valid handle before the DPR files main
>block is entered. A forms handle will be created when code first refers to it,

[snip]
Now that is _interesting_.

Peter, I can't thank you enough. This thread has opened up a host of
possibilities.

Sincerely,
--
Bill Boulton
using D3 Std on a 98SE box

Other Threads