471,594 Members | 1,919 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,594 software developers and data experts.

Use Form Constructor or Load event?

I was originally taught to double click the form, to get to the load
event handler and in there put anything I want to happen when my form
is opened.

However, today whilst reorganising some code for clarity it struck me
that I could just have well put this code in the forms constructor. The
code simply populates text boxes etc... on the form with values.

So i wondered as a rule of thumb is it generally better to do things
like populating text boxes on the form in the forms constructor, or in
the forms load event?

Thanks for your thoughts,

Gary-

Jan 5 '07 #1
8 3018
<ga********@myway.coma écrit dans le message de news:
11*********************@s34g2000cwa.googlegroups.c om...

|I was originally taught to double click the form, to get to the load
| event handler and in there put anything I want to happen when my form
| is opened.
|
| However, today whilst reorganising some code for clarity it struck me
| that I could just have well put this code in the forms constructor. The
| code simply populates text boxes etc... on the form with values.
|
| So i wondered as a rule of thumb is it generally better to do things
| like populating text boxes on the form in the forms constructor, or in
| the forms load event?

If you want to be a mouse jockey, then you use event handlers for
everything. However, when you learn to be a real programmer with OO skills,
then you tend to use the constructors and methods of the form classes just
like you would in any other, 'non-visual' class.

Your question indicates that you are no longer content with simply riding a
mouse :-)

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Jan 5 '07 #2
You can't put code that refers to the form's controls in the form's
constructor because, at that point in the construction process, the form's
controls don't exist yet. That's why the load event is so much more
commonly used, at that point, the form and its constituent controls are all
instantiated and ready to be accessed.

<ga********@myway.comwrote in message
news:11*********************@s34g2000cwa.googlegro ups.com...
>I was originally taught to double click the form, to get to the load
event handler and in there put anything I want to happen when my form
is opened.

However, today whilst reorganising some code for clarity it struck me
that I could just have well put this code in the forms constructor. The
code simply populates text boxes etc... on the form with values.

So i wondered as a rule of thumb is it generally better to do things
like populating text boxes on the form in the forms constructor, or in
the forms load event?

Thanks for your thoughts,

Gary-

Jan 5 '07 #3
Hi Gary,

You can place code that accesses the components on your form in the constructor
provided that it is after the call to InitializeComponent within your constructor.
That's the call that creates of all controls on the form and sets their properties.
However, occassionally you'll run into an action (more often with third-party
controls) that can only be done in the Form_Load event. For example, I was
working with a third-party control that provided methods to save and restore
the state of the control. I found that the best place to call the restore
method was from the Load event (didn't work in the constructor) and the best
place to call the save method was from the Closing event. So, go ahead and
use the constructor but tuck the knowledge of the Load event away in case
you run into some odd control that requires this at some point. Personally,
I tend to just use the Load event just to be on the safe side. As Joanna
implied, you could also override the Form's OnLoad method.

Best Regards,
Dustin Campbell
Developer Express Inc.
Jan 5 '07 #4
You can't put code that refers to the form's controls in the form's
constructor because, at that point in the construction process, the
form's controls don't exist yet.
Not entirely true; as long as your code *follows* InitialiseComponent,
then they should exist. Unless you currently use the Load event to
create them, but that isn't the IDE's approach...

Marc
Jan 5 '07 #5
Hi,

<ga********@myway.comwrote in message
news:11*********************@s34g2000cwa.googlegro ups.com...
>I was originally taught to double click the form, to get to the load
event handler and in there put anything I want to happen when my form
is opened.

However, today whilst reorganising some code for clarity it struck me
that I could just have well put this code in the forms constructor. The
code simply populates text boxes etc... on the form with values.

So i wondered as a rule of thumb is it generally better to do things
like populating text boxes on the form in the forms constructor, or in
the forms load event?
I tend to create a method to do that, or in another word a method to refresh
the interface. Inside the method I will populate the controls as they should
be in the initial state.

From where I call it initialy depends, I try to call it from the
constructor, but sometimes I just do the double click :) and it ends in the
Form_Load method.
Just remember that in the constructor it should be AFTER the
InitializeComponent() call

Also note that the Load event only runs once (as the contructor), if the
form is hidden and shown again the Load is not fired.

--
Ignacio Machin
machin AT laceupsolutions com
Jan 5 '07 #6
Additinal to what others said:
You should put code in the Load event, if it shouldn't run in design mode
because the DesignMode property doesn't work in the constructor.
But this more refers to controls then to forms; for forms this only matters,
if you want to derive from that form via the forms designer.

<ga********@myway.comschrieb im Newsbeitrag
news:11*********************@s34g2000cwa.googlegro ups.com...
>I was originally taught to double click the form, to get to the load
event handler and in there put anything I want to happen when my form
is opened.

However, today whilst reorganising some code for clarity it struck me
that I could just have well put this code in the forms constructor. The
code simply populates text boxes etc... on the form with values.

So i wondered as a rule of thumb is it generally better to do things
like populating text boxes on the form in the forms constructor, or in
the forms load event?

Thanks for your thoughts,

Gary-

Jan 5 '07 #7
Yes, entirely true since the constructor is the very first event to fire.

"Marc Gravell" <ma**********@gmail.comwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
>You can't put code that refers to the form's controls in the form's
constructor because, at that point in the construction process, the
form's controls don't exist yet.

Not entirely true; as long as your code *follows* InitialiseComponent,
then they should exist. Unless you currently use the Load event to create
them, but that isn't the IDE's approach...

Marc

Jan 5 '07 #8
I'm afraid that I am going to have to argue with you here. Read again:
*follows* InitialiseComponent [sic; pick "s" or "z" as you like]. In
IDE genereated classes (as suggested by the OP), then it is
InitializeComponent that creates the .Net classes representing the
controls, intialises the backing fields, and adds the controls to the
Controls collection. As such, in pretty much every way that matters
(for general coding) those controls now exist fully in a .Net sense,
and can be fully manipulated as required. The only thing that doesn't
(by default) exist at this point is the Win32 handles, but this is not
required for most functions (hooking events, changing properties etc).
So OK: you wouldn't be able to do backhand PInvoke on the controls yet,
and you won't be able to call BeginInvoke, but I don't recall that
being suggested.

Marc

Jan 6 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Dennis | last post: by
1 post views Thread by tony | last post: by
10 posts views Thread by menashay | last post: by
reply views Thread by =?Utf-8?B?TW9ydGVuIFdlbm5ldmlrIFtDIyBNVlBd?= | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by Anwar ali | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.