473,320 Members | 2,027 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,320 software developers and data experts.

load event vs constructor?

Hi there.
I'm doing an application in C# 2005.
I was used in VB6 to put code in Forms load event in order to initialize
some values of the controls (grid values, text boxes values, label captions,
among others). Sometimes that code includes retrieving information from a
database.

Now that I'm doing this in C# 2005, I realized that I can put that code
either in Form load event or in the constructor of the form after the
InitializeComponent(). I have tested both and they work exactly the same
(same results without runtime errors). So, at this point I think I need some
technical advise on why one is better than the other and which one is
recommended whereas the code I put in includes retrieving info from a
database.

Thanks in advance

Regards
Oct 13 '06 #1
6 11398
The constructor is executed very early in the form lifecycle, before
any of the form's events (including Load) are fired. The form won't be
completely initialised, so there will be some limitations to what you
can do.

Rolandpish wrote:
Hi there.
I'm doing an application in C# 2005.
I was used in VB6 to put code in Forms load event in order to initialize
some values of the controls (grid values, text boxes values, label captions,
among others). Sometimes that code includes retrieving information from a
database.

Now that I'm doing this in C# 2005, I realized that I can put that code
either in Form load event or in the constructor of the form after the
InitializeComponent(). I have tested both and they work exactly the same
(same results without runtime errors). So, at this point I think I need some
technical advise on why one is better than the other and which one is
recommended whereas the code I put in includes retrieving info from a
database.

Thanks in advance

Regards
Oct 13 '06 #2
Thanks for your reply Chris.
Well, I'll still be using form's load event to put that code.

Regards

"Chris Fulstow" wrote:
The constructor is executed very early in the form lifecycle, before
any of the form's events (including Load) are fired. The form won't be
completely initialised, so there will be some limitations to what you
can do.

Rolandpish wrote:
Hi there.
I'm doing an application in C# 2005.
I was used in VB6 to put code in Forms load event in order to initialize
some values of the controls (grid values, text boxes values, label captions,
among others). Sometimes that code includes retrieving information from a
database.

Now that I'm doing this in C# 2005, I realized that I can put that code
either in Form load event or in the constructor of the form after the
InitializeComponent(). I have tested both and they work exactly the same
(same results without runtime errors). So, at this point I think I need some
technical advise on why one is better than the other and which one is
recommended whereas the code I put in includes retrieving info from a
database.

Thanks in advance

Regards

Oct 13 '06 #3

Rolandpish wrote:
Hi there.
I'm doing an application in C# 2005.
I was used in VB6 to put code in Forms load event in order to initialize
some values of the controls (grid values, text boxes values, label captions,
among others). Sometimes that code includes retrieving information from a
database.

Now that I'm doing this in C# 2005, I realized that I can put that code
either in Form load event or in the constructor of the form after the
InitializeComponent(). I have tested both and they work exactly the same
(same results without runtime errors). So, at this point I think I need some
technical advise on why one is better than the other and which one is
recommended whereas the code I put in includes retrieving info from a
database.
Generally speaking, your constructor should run as quickly as possible
and return a constructed object to the caller as quickly as possible.
Certainly you don't want to start background threads and wait on them
in a constructor.

And that's how you should do database calls in a WinForms application:
you should do it on a background thread. Many of my Load events are
split into three methods: OnLoad(), which runs on the UI thread, sets
up bindings for my control and does other setup, disables the entire
form (so the user can't mess with it), then launches BackgroundLoad(),
which runs on a background thread and does the database fetches.
BackgroundLoad() then invokes FinishLoad() on the UI thread again to
re-enable the form now that the loading is complete.

The net effect is that while the application is fetching from the
database the window is still responsive: you can drag it and minimize
it. If I were to do the database fetch without a background thread then
the UI would completely "freeze" until the database call was complete,
which is not nice WinForms etiquette.

So... do the minimum you have to in the constructor, and do the heavy
lifting in OnLoad. Even if you don't bother doing proper threading, at
least you're set up to do it later.

Oct 13 '06 #4
Rolandpish,

I agree with Bruce on this one. Best practices say to avoid doing much
work in a constructor. I see no compelling reason to make an exception
for a Form.

Brian

Rolandpish wrote:
Hi there.
I'm doing an application in C# 2005.
I was used in VB6 to put code in Forms load event in order to initialize
some values of the controls (grid values, text boxes values, label captions,
among others). Sometimes that code includes retrieving information from a
database.

Now that I'm doing this in C# 2005, I realized that I can put that code
either in Form load event or in the constructor of the form after the
InitializeComponent(). I have tested both and they work exactly the same
(same results without runtime errors). So, at this point I think I need some
technical advise on why one is better than the other and which one is
recommended whereas the code I put in includes retrieving info from a
database.

Thanks in advance

Regards
Oct 14 '06 #5

Bruce Wood wrote:
Rolandpish wrote:
Hi there.
I'm doing an application in C# 2005.
I was used in VB6 to put code in Forms load event in order to initialize
some values of the controls (grid values, text boxes values, label captions,
among others). Sometimes that code includes retrieving information from a
database.

Now that I'm doing this in C# 2005, I realized that I can put that code
either in Form load event or in the constructor of the form after the
InitializeComponent(). I have tested both and they work exactly the same
(same results without runtime errors). So, at this point I think I need some
technical advise on why one is better than the other and which one is
recommended whereas the code I put in includes retrieving info from a
database.

Generally speaking, your constructor should run as quickly as possible
and return a constructed object to the caller as quickly as possible.
Certainly you don't want to start background threads and wait on them
in a constructor.

And that's how you should do database calls in a WinForms application:
you should do it on a background thread. Many of my Load events are
split into three methods: OnLoad(), which runs on the UI thread, sets
up bindings for my control and does other setup, disables the entire
form (so the user can't mess with it), then launches BackgroundLoad(),
which runs on a background thread and does the database fetches.
BackgroundLoad() then invokes FinishLoad() on the UI thread again to
re-enable the form now that the loading is complete.

The net effect is that while the application is fetching from the
database the window is still responsive: you can drag it and minimize
it. If I were to do the database fetch without a background thread then
the UI would completely "freeze" until the database call was complete,
which is not nice WinForms etiquette.

So... do the minimum you have to in the constructor, and do the heavy
lifting in OnLoad. Even if you don't bother doing proper threading, at
least you're set up to do it later.
It occurred to me this weekend that this is such a common
problem--having to do heavy lifting to set up a form--that one might as
well create a base Form class that exposes three protected methods:
LoadStart(), which runs on the UI thread; LoadBackground(), which runs
on a background thread; and LoadFinish() which runs on the UI thread
again. It would be pretty easy to do it once and then just inherit from
that and put the correct thing in the correct method.

I'm going to set up my forms that way when I'm back on Monday. :-)

Oct 15 '06 #6
Additional to what others said:
In the constructor the DesignMode property doesn't work.
So every behavior that depends on DesignMode mustn't be done in the
constructor.

"Rolandpish" <Ro********@discussions.microsoft.comschrieb im Newsbeitrag
news:5E**********************************@microsof t.com...
Hi there.
I'm doing an application in C# 2005.
I was used in VB6 to put code in Forms load event in order to initialize
some values of the controls (grid values, text boxes values, label
captions,
among others). Sometimes that code includes retrieving information from a
database.

Now that I'm doing this in C# 2005, I realized that I can put that code
either in Form load event or in the constructor of the form after the
InitializeComponent(). I have tested both and they work exactly the same
(same results without runtime errors). So, at this point I think I need
some
technical advise on why one is better than the other and which one is
recommended whereas the code I put in includes retrieving info from a
database.

Thanks in advance

Regards

Oct 16 '06 #7

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

Similar topics

6
by: Robert Werner | last post by:
I want to write one general purpose set of code that will handle the population (where required) of all controls on a form. Though I'm new to C# my instinct is to somehow hook into the Load event...
1
by: WFB | last post by:
Hi, I have a base class from which all of my pages derive (ABCBasePage). For example, ABCCustomerSelect Inherits ABCPasePage. I would now like to have ABCPocketSelect which should inherit from...
0
by: Frank 'Olorin' Rizzi | last post by:
Hello everyone. This is quite convoluted, but I'll try to make it simple. I have a couple of bottom-line questions (I guess): 1~ what happens between the Page_Load routine in the code behind...
4
by: C M Shaw | last post by:
I have a form which I want to show modally; it's a fairly old form that's been ported up several versions of VB, and I'd like to keep its rewriting to a minimum. Basically, it is used in this...
5
by: Engineerik | last post by:
I have a login form which I want to display with the showdialog method. When I do that the event handler for the load event does not run. If I use the show method the load event handler runs...
3
by: Dennis | last post by:
I have the following code for showing a form: dim frm as new myForm frm.ShowDialog (The Form Load Event is fired then I hide the form using Me.Hide when the X in the UR corner is clicked). ...
2
by: Franky | last post by:
Threre is a Form containing a usercontrol In the form's Load event it references a usercontrol property, say, zz The first showdialog(formx) causes 1 usercontrol_load event 2 form_load event...
8
by: garyusenet | last post by:
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...
3
by: GauravGupta | last post by:
i want to know that is it posible to call button click event before page load event on post back.... please help me....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.