0

From other SO questions that I have read it seems like using an override to onLoad is a better way to go instead of handling initial methods in the constructor..

What I'm finding when I put break points into my code is that the onLoad method is getting hit but the constructor isn't.. my form is obviously being opened fine so I'm wondering if anyone is able to shed any light into the order in which these happen?

This is the same for a custom load event also

EDIT: idiocy is to the reason why my breakpoint wasn't getting hit but I'm still a little confused as to when the on load override is called?

Sayse
  • 42,633
  • 14
  • 77
  • 146
  • Can u provide a code example ? – Dimitar Tsonev Dec 13 '12 at 13:25
  • Does it need one? it isn't code specific I'm just wondering about at what point the on load override is called (i.e when form is flagged as created or similar) – Sayse Dec 13 '12 at 13:28
  • Possible duplicate of [Winforms Form Constructor vs Load event](http://stackoverflow.com/questions/264396/winforms-form-constructor-vs-load-event) – Jim Fell Jul 07 '16 at 14:36
  • @JimFell - I'm curious, is there any reason you're trying to close 4 year old questions as duplicates of that one? – Sayse Jul 07 '16 at 14:38

1 Answers1

5

The OnLoad method/event is executed when the form is being shown for the first time.

The constructor is always called when you use the new keyword to create a new instance of your Form's class.

It is generally considered best practice to do all form initialization within the constructor, and not during OnLoad. If you have more than one constructor and want to perform some common initialization tasks within all of them, put them in a separate method and call it from your constructor. Make sure InitializeComponent() is called as part of your constructor. OnLoad should really only be used if you need to re-position your form or something.

tom.dietrich
  • 8,219
  • 2
  • 39
  • 56
  • Hmm, well in my case my form has multiple tabs on it and having all of these initialize at once is kind of unnecessary (initializing a form with tabs will initialize all tabs) its good to know exactly when these events are called though so thanks! – Sayse Dec 13 '12 at 13:43