[fpc-pascal] When is TComponent.Loaded called?

Michael Van Canneyt michael at freepascal.org
Mon Sep 17 13:34:08 CEST 2007



On Mon, 17 Sep 2007, Graeme Geldenhuys wrote:

> Hi,
> 
> As the subject says. When is TComponent.Loaded called?  I'm trying to
> use it in fpGUI to initialize a few things in a component, but Loaded
> never seems to execute.
> 
> Reading the Kylix 3 help, it says Loaded is called by the streaming
> system that loads the 'form files'.  Well fpGUI doesn't use 'form
> files' (like *.dfm or *.lfm). Instead the GUI Designer creates the
> components via code in a normal *.pas unit, which is called after the
> Form's constructor has completed.
> 
> I gather it is safe to assume the FCL never calls a TComponent's
> Loaded method anywhere?

It is called in the streaming code, classes.inc in 
TReader.ReadRootComponent

It's used to perform certain actions which should only be done after 
all properties are loaded.

You should never call loaded directly (in fact, you can't since it is
protected).

The reason for it's presence is simple: you can't force the order of
loading properties. Suppose you have a dataset component: the active
property is one of the first to be set: none of the properties which
actually matter (SQL, Database etc) are set, so putting active to True would
raise an error. Therefore the action of setting 'Active' is deferred till 
the Loaded call: then the needed properties are normally all set.

When creating components in code, it's assumed that you set them in the
proper order prior to setting Active.

Michael.



More information about the fpc-pascal mailing list