6 Replies - 1362 Views - Last Post: 03 February 2012 - 11:43 PM

Topic Sponsor:

#1 negligible  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 56
  • View blog
  • Posts: 272
  • Joined: 02-December 10

Form() vs Form_Load(object, sender)

Posted 31 January 2012 - 04:44 AM

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
Is This A Good Question/Topic? 0
  • +

Replies To: Form() vs Form_Load(object, sender)

#2 RexGrammer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 152
  • View blog
  • Posts: 664
  • Joined: 27-October 11

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.
Was This Post Helpful? 1
  • +
  • -

#3 negligible  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 56
  • View blog
  • Posts: 272
  • Joined: 02-December 10

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.
Was This Post Helpful? 0
  • +
  • -

#4 RexGrammer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 152
  • View blog
  • Posts: 664
  • Joined: 27-October 11

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.
Was This Post Helpful? 1
  • +
  • -

#5 tlhIn`toq  Icon User is online

  • WillMyCodeWork = !FailedWhenYouTriedIt;
  • member icon

Reputation: 3290
  • View blog
  • Posts: 6,897
  • Joined: 02-June 10

Re: Form() vs Form_Load(object, sender)

Posted 31 January 2012 - 07:41 AM

View Postnegligible, 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.
Was This Post Helpful? 2
  • +
  • -

#6 tlokzz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 05-October 09

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.
Was This Post Helpful? 0
  • +
  • -

#7 logicPwn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 03-February 12

Re: Form() vs Form_Load(object, sender)

Posted 03 February 2012 - 11:43 PM

I use both just like tlokzz said
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1