8 Replies - 8279 Views - Last Post: 30 May 2012 - 09:35 AM

#1 negligible  Icon User is offline

  • D.I.C Regular

Reputation: 62
  • View blog
  • Posts: 302
  • 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

  • Coding Dynamo
  • member icon

Reputation: 181
  • View blog
  • Posts: 777
  • 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

Reputation: 62
  • View blog
  • Posts: 302
  • 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

  • Coding Dynamo
  • member icon

Reputation: 181
  • View blog
  • Posts: 777
  • 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 offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5529
  • View blog
  • Posts: 11,839
  • 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? 3
  • +
  • -

#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
  • +
  • -

#8 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3576
  • View blog
  • Posts: 11,125
  • Joined: 05-May 12

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

Posted 28 May 2012 - 07:59 PM

Also beware that exceptions thrown during Load event just go to limbo. (I can't find the link right now, but this is known and by design behavior, believe it or not.)

I prefer to have variable initialization and form control construction in the constructor, and initial logic that loads defaults into controls in the Load event, and any thing that plays with input focus in the Shown event.

(I maybe a weird duck as well since given a choice, I write my forms by hand rather than use the Designer. Unfortunately, this does not play well with others when other people open up my code in Visual Studio and the Designer decides to muck with my code. I just grin at the others and say "You broke it. You fix it. Code was working fine when I checked it in.")
Was This Post Helpful? 0
  • +
  • -

#9 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4490
  • View blog
  • Posts: 7,822
  • Joined: 08-June 10

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

Posted 30 May 2012 - 09:35 AM

Quote

I just grin at the others and say "You broke it. You fix it. Code was working fine when I checked it in."


I'm sure that's a bit tongue-in-cheek, but to be honest, teamwork is more important than being right. If your whole team uses the designer and can't edit your forms, it's more practical for you to change then for everyone else to change.

Edit: that came off a bit high-handed. I don't mean to insinuate that you're not a good team player. I just have worked with people that did things their way, regardless that the team had already agreed on a different style.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1