In the Load event or in the constructor after the Initiali开发者_C百科zeComponent()?
or doesn't matter at all?
The Form.Shown event is a good place to do any initialization that may take more than about one second. Form.Shown occurs once only, just after the form is first made visible to the user.
Obviously if you have a lengthy initialization you still need to provide some sort of visual feedback and perhaps disable sections of the form until complete. But if the initialization is unavoidable, Form.Shown at least allows you to let the user know the application has not frozen and give feedback on what it is actually doing.
Compared to Form.Load: From the users perspective your application will be perceived to start up more quickly because your Form is already visible while initialization is done.
Compared to Form.Activated: You don't need to worry about your initialization running multiple times because the Activated event is called every time your form is hidden/shown, minimized/maximized etc.
Compared to the constructor: Similar to Form.Load your form will not be visible until initialization is complete. Also, you must be more careful about timing/sequence issues related to controls that may not be fully initialized.
For heavier initialization it's typically done in the load event. Constructors are usually used for quick, simple initialization of fields. If you have to make a method call to an external dependency for example, you should do it from load.
Heavier initialization may also not be a good idea in load event as it may increase load time and can be irritating to end user. I prefer doing a basic initialization (say..which takes <10 seconds) in Form Load and do rest of the heavy work after it is displayed to the user. To make user wait a progress indicator can be displayed.
It depends on the kind of initialisation. Simple field initialisation, for example, can be done in the constructor and this saves you from having to hook up an event, have an additional method, etc.
However, in some cases the constructor doesn't have the info you need. For example, if you want to do different things depending on whether you're in design mode or run mode (e.g. in run mode you will connect to a data source, but in design mode you want to display sample data), then this must be deferred until after construction, because the framework doesn't set DesignMode until after the object has been constructed.
Doing stuff in the Load event will make the exceptions when it fails slightly easier to read and IMHO is semantically cleaner. Practically, it doesn't make a ton of difference.
Think of what will happen if you run Show
or ShowDialog
for the same Form twice. All things that cannot change between those two calls should be in the constructor; all initialization code depending on the according Show(Dialog) call have to be in an event handler that will be called for each of those calls. For example, the owner of the Form is be passed in ShowDialog
, not in the constructor, and can be different for 2 different ShowDialog
calls, so everything what depends on the owner should not be in in the constructor.
精彩评论