[Lazarus] Accesing components in FormCreate event

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

[Lazarus] Accesing components in FormCreate event

Lars
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.

If any of you know, as this is definitely a been there done that
experience thing... it may have bitten someone else. The problem is it's
been a while since I was bitten by a bug similiar to this so I cannot
remember. Even putting the code in the formshow event, I am not certain
solves all problems, using a counter and on first show only.

It may have also been just a bug with certain components, but not others,
and it may have been an intermittent bug. This, if not already written
about, would be a good article for the wiki or somewhere "best practices"
when creating a form or changing components at run time.
--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus-ide.org/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] Accesing components in FormCreate event

Michael Van Canneyt


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.
OnShow may be called multiple times in the lifetime of a form.

Michael.
--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus-ide.org/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] Accesing components in FormCreate event

Mattias Gaertner
On Thu, 22 Sep 2016 07:36:45 +0200 (CEST)
Michael Van Canneyt <[hidden email]> 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
--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus-ide.org/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] Accesing components in FormCreate event

Lars
On Thu, September 22, 2016 12:48 am, Mattias Gaertner wrote:
>> No, formcreate is the correct event to set up new components.
>>
>
> Yes.

Prefer positive attitude of Yes, vs No. Inbetween Qbit state: maybe.

> 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.


Maybe these were some of the issues. What if you had three panels, and you
set all the alignments up.. then when the form is finally shown it has no
idea which alignment you wanted done first?  Or does it somehow memorize
which alignments were set first.  This could be related to the issue I had
but cannot remember, for the life of me.
--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus-ide.org/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] Accesing components in FormCreate event

Mattias Gaertner
On Thu, 22 Sep 2016 00:54:35 -0600
"Lars" <[hidden email]> wrote:

>[...]
> Prefer positive attitude of Yes, vs No. Inbetween Qbit state: maybe.

I meant 'True'.

 

> > 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.  
>
>
> Maybe these were some of the issues. What if you had three panels, and you
> set all the alignments up.. then when the form is finally shown it has no
> idea which alignment you wanted done first?  Or does it somehow memorize
> which alignments were set first.  This could be related to the issue I had
> but cannot remember, for the life of me.

Maybe this helps:
http://wiki.lazarus.freepascal.org/Autosize_/_Layout#Order_of_controls_with_same_Align

Mattias
--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus-ide.org/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] Accesing components in FormCreate event

Free Pascal - Lazarus mailing list
You're thinking of OnActivate.  OnShow shows only once, after OnCreate.  See: http://wiki.lazarus.freepascal.org/Event_order

Bob B.


On Thursday, September 22, 2016 12:17 AM, Mattias Gaertner <[hidden email]> wrote:


On Thu, 22 Sep 2016 00:54:35 -0600
"Lars" <[hidden email]> wrote:

>[...]
> Prefer positive attitude of Yes, vs No. Inbetween Qbit state: maybe.

I meant 'True'.


> > 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. 
>
>
> Maybe these were some of the issues. What if you had three panels, and you
> set all the alignments up.. then when the form is finally shown it has no
> idea which alignment you wanted done first?  Or does it somehow memorize
> which alignments were set first.  This could be related to the issue I had
> but cannot remember, for the life of me.

Maybe this helps:
http://wiki.lazarus.freepascal.org/Autosize_/_Layout#Order_of_controls_with_same_Align


Mattias
--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus-ide.org/listinfo/lazarus



--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus-ide.org/listinfo/lazarus