Hey Chaps,
Rewriting someone else's old code at the moment which has raised a question in my brain.
Why would you put code in Form1_Load event handler? When Form1() is going to be run first on load anyway?
I figure .NET do implement most things for a reason. Is there a good one for splitting the code like this? Threading?
Thanks
Form() vs Form_Load(object, sender)
Page 1 of 16 Replies - 1362 Views - Last Post: 03 February 2012 - 11:43 PM
Topic Sponsor:
Replies To: Form() vs Form_Load(object, sender)
#2
Re: Form() vs Form_Load(object, sender)
Posted 31 January 2012 - 05:42 AM
So you're wondering why forms have a Load event when they have a constructor?
Well the reason is simple: the constructor first initializes the form object, which before initialization was just free memory on the heap. Then the Form_Load event is executed. So before initialization (which is done by the constructor) no event can be executed.
A plus reason is just logic: the constructor is used to initialize some values, while the event is used for the program logic.
Well the reason is simple: the constructor first initializes the form object, which before initialization was just free memory on the heap. Then the Form_Load event is executed. So before initialization (which is done by the constructor) no event can be executed.
A plus reason is just logic: the constructor is used to initialize some values, while the event is used for the program logic.
#3
Re: Form() vs Form_Load(object, sender)
Posted 31 January 2012 - 06:12 AM
Ok, so it doesn't make any difference aside from organisational preferences. Thanks.
#4
Re: Form() vs Form_Load(object, sender)
Posted 31 January 2012 - 06:21 AM
And the point that without the InitializeComponent() in the constructor you wouldn't have a form. Also the constructor does something behind the curtains which is also important, it fills the memory in the heap needed for the class.
#5
Re: Form() vs Form_Load(object, sender)
Posted 31 January 2012 - 07:41 AM
negligible, on 31 January 2012 - 07:12 AM, said:
Ok, so it doesn't make any difference aside from organisational preferences. Thanks.
No no no. it DOES make a difference. And there is another event to consider... Form_shown
First a form (or any class) is constructed. At this point it is just manufactured. But it has not presence. No .Location, .Size, .Width etc. Notice that the constructor of a form calls the InitializeComponents() method from the designer.cs file. You could in theory make/set a bunch of property values before the GUI parts are ever created.
Then a form is loaded. So now it has a physical presence. You can do things with the GUI elements of the form because they exist now. Maybe you want to adjust the width of a combobox to better adjust for the largest element in it.
Then the form is Shown. This happens when it is displayed on screen. Maybe this is where you start a timer, because the user has x seconds to do something once they can see the form.
#6
Re: Form() vs Form_Load(object, sender)
Posted 03 February 2012 - 08:01 PM
So what do you guys prefer?
In form load event, or after InitializeComponent() in the constructor?
For me, I like both I guess. Depends on what I'm going to be doing on initialization. Like said above, I like to put programming in the load event and variable setting in the constructor.
In form load event, or after InitializeComponent() in the constructor?
For me, I like both I guess. Depends on what I'm going to be doing on initialization. Like said above, I like to put programming in the load event and variable setting in the constructor.
#7
Re: Form() vs Form_Load(object, sender)
Posted 03 February 2012 - 11:43 PM
I use both just like tlokzz said
Page 1 of 1
|
|

New Topic/Question
Reply


MultiQuote







|