Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2016-09

Accesing components in FormCreate event

6 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

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.

Reply by email

Michael Van Canneyt
to Lars
Lars wrote 14 lines
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.

Reply by email

Mattias Gaertner
to Michael Van Canneyt
Show quoted text 18 lines
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

Reply by email

Lars
to Mattias Gaertner
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.

Reply by email

Mattias Gaertner
to Lars
[...] Prefer positive attitude of Yes, vs No. Inbetween Qbit state: maybe.

I meant 'True'.

Show quoted text 11 lines
> 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

Reply by email

Bob B.
to Mattias Gaertner

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 wrote:

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

I meant 'True'.

Show quoted text 11 lines
> 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

Reply by email