473,414 Members | 1,606 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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

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
Sep 14 '06 #1
6 2135
"tony" <jo*****************@telia.comwrote in message
news:uZ**************@TK2MSFTNGP03.phx.gbl...
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
Sep 14 '06 #2
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.

"tony" <jo*****************@telia.comwrote in message
news:uZ**************@TK2MSFTNGP03.phx.gbl...
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


Sep 14 '06 #3
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?
Sep 14 '06 #4
"John Brown" <no_spam@_nospam.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
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
Sep 15 '06 #5
"Kevin Spencer" <uc*@ftc.govwrote in message
news:um**************@TK2MSFTNGP03.phx.gbl...
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
Sep 15 '06 #6
>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
Thanks for the confirmation :)
Sep 15 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Derrick | last post by:
Long story short: I've been working on a project which includes both designtime and runtime components, for both the PC and Pocket PC. While testing, I've been having problems with Visual Studio...
15
by: Steve | last post by:
I have a form with about 25 fields. In the BeforeUpdate event of the form, I have code that sets the default value of each field to its current value. For a new record, I can put the focus in any...
2
by: Nony Buz | last post by:
My objective is simply: Notify a form when a image file has been created in a directory. First there is a basic interface for the callback function: public interface INewImageNotify { void...
7
by: Dica | last post by:
i've used the sample code from msdn to create an encyption/decryption assembly as found here: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetsec/html/SecNetHT10.asp i'm...
13
by: Duron | last post by:
I created a new folder using VS.NET 2003. Then I created a new web form under that folder, say, \Member\Default.aspx. However, even if I didn't do anything to that page, a run-time error appears...
7
by: Drew Berkemeyer | last post by:
I've encounted a pretty strange problem and I'm not quite sure what to make of it. I have a web service that consumes an XML file as well as a few other parameters. This web service works fine...
21
by: StriderBob | last post by:
Situation : FormX is mdi child form containing 2 ListViews ListView1 contains a list of table names and 4 sub items with data about each table. ListView2 contains a list of the columns on each...
44
by: Josh Mcfarlane | last post by:
Just out of curiosity: When would using std::list be more efficient / effective than using other containers such as vector, deque, etc? As far as I'm aware, list doesn't appear to be...
34
by: Tom | last post by:
I'd greatly appreciate advice and code snippets on how to create a ram disk within a C/C++ program. I also need to be able to determine the free space. Thanks in advance for any help.
13
JodiPhillips
by: JodiPhillips | last post by:
G'day, I have a silly and simple problem that I need some guidance with. Due to the way our network is set up, I am unable to use the group permissions for Access and have had to implement log...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.