40

Whats the difference between a form constructor and the form_Load method?

Whats your though process for placing items in one vs the other?

Ryan
  • 4,443
  • 4
  • 26
  • 26

2 Answers2

37

Don't use the Load event, override the OnLoad() method. That ensures that everything runs in a predictable order when you derive from the form class. You should only use it for form initialization that requires the size of the actual form to be know. It can be different from the design size due to scaling or user preferences and the actual size isn't know until the native window is created.

Initializing controls in the OnLoad method is possible but it can be very slow, especially for ListView and TreeView. If you initialize them in the constructor, they can be bulk-initialized when their native Windows controls are created.

One special exception: creating an MDI child window should always be done in OnLoad(), there's a bug in the plumbing code that messes up the MDI bar when you create a child in the constructor.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
26

Code in the constructor runs immediately when you create the form, whether or not you ever display it. Code running in the Form.Load event is an event handler, so you can actually have code in other classes (which have subscribed to the form) run code there. Similarly, you can (from the form) use the Form.OnLoad method to run code.

The form's Load event (and OnLoad overridable method, which is often a better choice in the form itself) runs after the form has been initialized. This often has advantages, since all of the form's controls have already been constructed, and more importantly, all of the form layout has occurred.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • Couple of things: 1- How often would you construct a form w/o showing it? (Esp if your dependencies are properly injected with delayed instansiation), 2- As noted by @Hans below, you incur a performance penalty for any control initialization at this point bc your layout is not suspended so all controls on the form are trying to respond to things like repaint events while being initialized. 3- I wouldn't use OnLoad to call my own code; rather create a method for MyAction() and call MyAction() from OnLoad- otherwise it's not clear to consumers that they need to call OnLoad to perform MyAction – Fred Sep 20 '12 at 16:02
  • 1
    @Fred Nothing you're arguing about conflicts with what I wrote. I specifically mentioned that OnLoad happens after initialization, and never suggested adding *controls* in OnLoad. As for your point 3- Consumers never "call OnLoad" - it happens in the runtime. If the code isn't going to be reused in any other locations, there is no distinct advantage to introducing another method. (I completely agree, however, if "MyAction" is something that will be reused, it should be in its own method - my answer doesn't preclude or suggest otehrwise, though) – Reed Copsey Sep 20 '12 at 16:07
  • Don't think adding controls- what about populating drop downs, list boxes, etc? I speak from experience it's slower to do in OnLoad. The point about MyAction was a (perhaps mis)-interpretation of the end of your 1st paragraph. Overall, I'd say a general rule-of-thumb is don't do anything "significant" in onload. The only valid action I can think of is something that requires knowledge of current layout info (e.g. size of form/pixel location of control). Note that this is pretty moot of you're following any presentation pattern (e.g. MVP) as your presenter should handle data initialization. – Fred Sep 20 '12 at 16:18
  • 1
    @Fred I agree - I just don't feel like I ever contradict any of that. My first paragraph (mostly) just discusses the mechanical differences between the two, in terms of when and where things occur. – Reed Copsey Sep 20 '12 at 16:27
  • I prefer to load the data (combobox value, bindingsource, etc) of the form in the sub which call the form. After the constructor but before the show instruction. For combobox which use fixed datasource (like a enum) I use the constructor instead. The only exception is the "startup" form when you want to keep the application Framework enabled. – Marco Guignard Aug 09 '16 at 14:23