[Lazarus] Accesing components in FormCreate event

Mattias Gaertner nc-gaertnma at netcologne.de
Thu Sep 22 08:48:09 CEST 2016


On Thu, 22 Sep 2016 07:36:45 +0200 (CEST)
Michael Van Canneyt <michael at freepascal.org> wrote:

> On Wed, 21 Sep 2016, Lars wrote:
> 
> > I'm trying to remember if setting up some values of components is okay to
> > do in the form create event. From what I remember in Delphi days of
> > programming years ago, I think I found out the hard way that setting up
> > components in the FormCreate event was a bad idea, and I had to resort to
> > tricks like doing it in the FormShow event instead, using a counter to
> > only do it on the first show.
> >
> > Do I recall correctly? Is it bad practice to set up some components on the
> > form in the formcreate event, because of timing and issues with the
> > components not all being set up in time?  I recall, but cannot be sure,
> > that in Delphi this led to issues and bugs where if you accessed
> > components on the form in the formcreate event, it was too soon for some
> > components as they had not initialized yet. I can't remember for sure
> > though, and Lazarus may be different.  
> 
> No, formcreate is the correct event to set up new components. 

Yes.
Keep in mind that the LCL tries to create the Handle and layout after
calling FormCreate. So changing properties do not have immediate
effects as in the VCL. For example changing Panel1.Align=alTop will
move Panel1 to top after FormCreate.


> OnShow may be called multiple times in the lifetime of a form.

You can use
Form1.AddHandlerFirstShow

Mattias


More information about the Lazarus mailing list