When to use form's Load event?

R

Rexel

In VB6 I usually use the form's events in the following order:

Initialize: To initialize variables if need be. Controls are not yet
loaded.

Load: Now that controls are loaded they are set up, e.g., combo
boxes are loaded, textboxes are filled with default values if needed,
controls are enabled/disabled according to the circumstanses.

Resize: My forms are all opened maximized, so this event is used
to resize the controls.

But in VB.NET it is different. I can put the Initialize code in the New
constructor. But it seems that controls are loaded there two, so I can
put the code I used to use in VB6 Load event in the New, after the
InitializeComponents.
Besides in VB.NET the Load event fires AFTER the Resize event.
So why or when do I need to use the Load event?
 
J

Jan Tielens

This is a typical constructor of a form:

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

The InitializeComponent method will put all controls on your form. So make
sure you add your code after the "'Add any initialization after the
InitializeComponent() call" line.

Here is a nice post which discusses the differences between constructors,
events, overriding, ...
http://weblogs.asp.net/savanness/archive/2003/03/10/3646.aspx

--
Greetz

Jan Tielens
________________________________
Read my weblog: http://weblogs.asp.net/jan
 
R

Rexel

This is a typical constructor of a form:

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

The InitializeComponent method will put all controls on your form. So make
sure you add your code after the "'Add any initialization after the
InitializeComponent() call" line.

Here is a nice post which discusses the differences between constructors,
events, overriding, ...
http://weblogs.asp.net/savanness/archive/2003/03/10/3646.aspx

Thanks for the reply.
So the answer to my question is that you don't need to use form's Load
event in VB.NET at all, whereas in VB6 I could hardly do without this
event.
 
H

Herfried K. Wagner [MVP]

* Rexel said:
Besides in VB.NET the Load event fires AFTER the Resize event.
So why or when do I need to use the Load event?

If you want something to take place directly before the form is shown.
 
J

Jan Tielens

Are you referring to the Form_Initialize sub? In my opinion that's the
closes you'll be able to get. There are some major limitations (e.g.
parameters).
--
Greetz

Jan Tielens
________________________________
Read my weblog: http://weblogs.asp.net/jan
 
H

Herfried K. Wagner [MVP]

* "Jan Tielens said:
Are you referring to the Form_Initialize sub? In my opinion that's the
closes you'll be able to get. There are some major limitations (e.g.
parameters).

Yep!
 
J

Jay B. Harlow [MVP - Outlook]

Rexel,
In addition to the other comments, I normally continue to use the Load event
for the same reasons you used it in VB6. Although it is not a requirement as
much as it was in VB6, it is logically a good place to put it.

Also I would recommend you use (try to use) the Docking & Anchoring
properties of the form instead of resizing in the Resize event. If Docking &
Anchoring does not work, I would recommend you change the layout of your
controls in the Layout event, not the Resize event.

The Resize event is raised when the form's size changes, the Layout event is
raised when child controls need to be repositioned or resized. Layout is
guarded by the SuspendLayout & ResumeLayout methods, where as the Resize
event always is unguarded... Again the "intended" place to put the logic...

Hope this helps
Jay
 
R

Rexel

If you want something to take place directly before the form is shown.

Well, yes. But can you give an example?
The more I think about it the more I come to the conclusion that the
Form_Load event has become irrelevant in VB.NET.
It seems to me that the old Initialize event and the Load event have
been rolled into the New method.
Is there any case that I want to put the code in the Load event, and
that I can't (or shouldn't) put it in the New constructor after the call to
the InitializeComponent?
Of course being new to VB.NET my reference is the old VB, and things
I used to do there. I am not yet familiar with all the VB.NET subtleties.
 
R

Rexel

Rexel,
In addition to the other comments, I normally continue to use the Load event
for the same reasons you used it in VB6. Although it is not a requirement as
much as it was in VB6, it is logically a good place to put it.

Also I would recommend you use (try to use) the Docking & Anchoring
properties of the form instead of resizing in the Resize event. If Docking &
Anchoring does not work, I would recommend you change the layout of your
controls in the Layout event, not the Resize event.

The Resize event is raised when the form's size changes, the Layout event is
raised when child controls need to be repositioned or resized. Layout is
guarded by the SuspendLayout & ResumeLayout methods, where as the Resize
event always is unguarded... Again the "intended" place to put the logic...

Hope this helps
Jay

Thanks for the advice. I will look into the Layout event.
 
C

Cor

Hi Rexel,

In addition to the others,

I found the load event in VB6 a big trouble.
And therefore I became a little bit angry from that event.

I use the load event in VB.net (2003) always in my programs, it is a nice
place to initialize things just before loading the form.

In a webform it is really the standard place to initialize.
(I always compare it with the HTML body tag with onload)

Just my 2 Eurocents

Cor
 

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