Load, OnLoad, Constructor

血红的双手。 提交于 2019-12-02 18:11:40

问题


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.



来源:https://stackoverflow.com/questions/13860609/load-onload-constructor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!