Load, OnLoad, Constructor

2020-05-08 06:09发布

问题:

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?

回答1:

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.