Use Form Constructor or Load event?

G

garyusenet

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

Joanna Carter [TeamB]

<[email protected]> a écrit dans le message de (e-mail address removed)...

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

Scott M.

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

Dustin Campbell

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
 
M

Marc Gravell

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
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

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

Christof Nordiek

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top