473,466 Members | 1,377 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Use Form Constructor or Load event?

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-

Jan 5 '07 #1
8 3131
<ga********@myway.coma écrit dans le message de news:
11*********************@s34g2000cwa.googlegroups.c om...

|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

--
Joanna Carter [TeamB]
Consultant Software Engineer
Jan 5 '07 #2
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.

<ga********@myway.comwrote in message
news:11*********************@s34g2000cwa.googlegro ups.com...
>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-

Jan 5 '07 #3
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.
Jan 5 '07 #4
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
Jan 5 '07 #5
Hi,

<ga********@myway.comwrote in message
news:11*********************@s34g2000cwa.googlegro ups.com...
>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.

--
Ignacio Machin
machin AT laceupsolutions com
Jan 5 '07 #6
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.

<ga********@myway.comschrieb im Newsbeitrag
news:11*********************@s34g2000cwa.googlegro ups.com...
>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-

Jan 5 '07 #7
Yes, entirely true since the constructor is the very first event to fire.

"Marc Gravell" <ma**********@gmail.comwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
>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

Jan 5 '07 #8
I'm afraid that I am going to have to argue with you here. Read again:
*follows* InitialiseComponent [sic; pick "s" or "z" as you like]. In
IDE genereated classes (as suggested by the OP), then it is
InitializeComponent that creates the .Net classes representing the
controls, intialises the backing fields, and adds the controls to the
Controls collection. As such, in pretty much every way that matters
(for general coding) those controls now exist fully in a .Net sense,
and can be fully manipulated as required. The only thing that doesn't
(by default) exist at this point is the Win32 handles, but this is not
required for most functions (hooking events, changing properties etc).
So OK: you wouldn't be able to do backhand PInvoke on the controls yet,
and you won't be able to call BeginInvoke, but I don't recall that
being suggested.

Marc

Jan 6 '07 #9

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

Similar topics

8
by: CJack | last post by:
hy, I have an mdi application, i create a child form and I want to know when a button is pressed while that child form is loaded. I have this code: private void frmTestBaby_KeyUp(object sender,...
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...
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). ...
1
by: tony | last post by:
Hello! I have a demo program that have this event handler Form1_Load below. I just wonder is it any difference if the call to this Form1_Load is removed and the call to InitGrid() is done in...
6
by: tony | last post by:
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...
13
by: cj | last post by:
In a project done in 2003 about a year ago I was told to add the SocketWrench code below into the Windows Form Designer generated code area as shown below. #Region " Windows Form Designer...
9
by: RvGrah | last post by:
I'm completely new to using background threading, though I have downloaded and run through several samples and understood how they worked. My question is: I have an app whose primary form...
10
by: menashay | last post by:
Hello, I am absolute beginner in C# so pardon me if the following question is too easy. I have a form with a one button. When I click the button I want to display a message that reads...
0
by: =?Utf-8?B?TW9ydGVuIFdlbm5ldmlrIFtDIyBNVlBd?= | last post by:
"Ron" wrote: Hi Ron, I'm afraid you won't get the snappy user interface you may get with native code. Event though you do all your creation in the constructor. A lot of code won't...
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
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,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.