When is it important to use form load compare to using C-tor

  • Thread starter Thread starter tony
  • Start date Start date
T

tony

Hello!

When exactly is it important or advisable to use this form load event
handler compare to using the C-tor.
For example here I create an event handler called dataBoundGridForm that is
called when the form is loaded.
this.Load += new System.EventHandler(this.dataBoundGridForm_Load);

I mean if I compare form load with the C-tor I think that it's enough to put
the code into the C-tor instead
of using the event handler for the form load.

So when can I skip form load and put the code into C-tor and
when is it important to use form load and put some code here.
If the form load is to be used what kind of functionallity should be put
here instead of using the C-tor.

//Tony
 
tony said:
Hello!

When exactly is it important or advisable to use this form load event
handler compare to using the C-tor.
For example here I create an event handler called dataBoundGridForm that
is
called when the form is loaded.
this.Load += new System.EventHandler(this.dataBoundGridForm_Load);

Events are for one object to revieve notifications from another, they are
not really for an object to revieve notifications from itself. If you use
form.Load you should use the override OnLoad.
I mean if I compare form load with the C-tor I think that it's enough to
put
the code into the C-tor instead
of using the event handler for the form load.

From my understanding the actual window itself is not created until after
the form's constructor has exited. So anything you do in the constructor is
being stored in the Form class and is being passed to the actual window
later. For example if you set the caption of the form it will be stored in
the form and later set using SetWindowText (or something similar) after the
window is created. This is probably no big deal but I tend to do these
things in OnLoad now just because it makes more sense to do it when the
window actually exists.

On the other hand some things cause the window handle to be destroyed and
recreated if done in Onload. I can't remember exactly what but say hiding
the control box requires the window to be recreated. If this is done in
Onload then the entire form will be destroyed and recreated. If you set
several similar properties then it could happen several times.
So when can I skip form load and put the code into C-tor and
when is it important to use form load and put some code here.
If the form load is to be used what kind of functionallity should be put
here instead of using the C-tor.

I'm pretty sure API functions that require the window handle won't work
there but besides that I think just about everything can go there.

Michael
 
It all depends on what you need in the Form. The Constructor is called
first, and calls InitializeComponent to instantiate the Controls of the
Form. Other code may be added as well. The OnLoad method is called when the
Form first appears. And I would override the OnLoad method, rather than
using the Load event handler if I were to use this, as it will be much
faster.

Bottom line is, it's all a matter of sequence. You don't want to access
anything that isn't ready. Whichever point you choose is up to you, other
than that issue.

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

What You Seek Is What You Get.
 
From my understanding the actual window itself is not created until after
the form's constructor has exited.

I'm fairly new to .NET but wouldn't the window itself be created only after
"Form.ShowDialog()" (and cousins ) is invoked? I'm guessing that "OnLoad()"
is really just the analogue for WM_INITDIALOG in the WinAPI (perhaps just a
wrapper for it on Windows itself). Can you or anyone else elaborate further?
 
John Brown said:
I'm fairly new to .NET but wouldn't the window itself be created only
after "Form.ShowDialog()" (and cousins ) is invoked? I'm guessing that
"OnLoad()" is really just the analogue for WM_INITDIALOG in the WinAPI
(perhaps just a wrapper for it on Windows itself). Can you or anyone else
elaborate further?

You are correct. Creating an instance of the form will not create the
underlying window until you call Show or ShowDialog. During the constructor
and InitializeComponent no window exists. Then when you call showDialog the
window handle gets created and OnLoad is called.

You can test this by overriding OnHandleCreated and putting a breakpoint on
that and ShowDialog and OnLoad. The order will be

Constructor
ShowDialog
OnHandleCreated
OnLoad

Michael
 
Kevin Spencer said:
It all depends on what you need in the Form. The Constructor is called
first, and calls InitializeComponent to instantiate the Controls of the
Form. Other code may be added as well. The OnLoad method is called when
the Form first appears.

I'm not sure appears is the correct word there. The form won't become
visible until after OnLoad completes. It has been created but is invisible
at that point.

Michael
 
I'm fairly new to .NET but wouldn't the window itself be created only
You are correct. Creating an instance of the form will not create the
underlying window until you call Show or ShowDialog. During the
constructor and InitializeComponent no window exists. Then when you call
showDialog the window handle gets created and OnLoad is called.

You can test this by overriding OnHandleCreated and putting a breakpoint
on that and ShowDialog and OnLoad. The order will be

Constructor
ShowDialog
OnHandleCreated
OnLoad

Thanks for the confirmation :)
 

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

Back
Top