Form_Load() ‘event’ or Override OnLoad()

You should always override OnLoad(). Using the event is only appropriate when another class would be interested in the event. Which is what events are for. Another class being interested in the Load event is very rare, only really useful to do window arrangement stuff.

Still, the Load event works well with the designer and VB6 programmers are very comfortable with it. It isn’t horribly wrong, you’d only get in trouble when you start inheriting the form and code doesn’t run in the right order.

Most code that now gets put in the Load event really belongs in the constructor. You only need OnLoad if:

  • You need to know the exact size and position of the window. OnLoad is best, the window Handle is created and the user preferences are applied (title and border size) and the form was rescaled as directed by the Form.AutoScaleMode property. The window is not yet visible, a very good time to move the window somewhere else or to arrange the child controls.
  • You have code that needs the Handle property. This is subtle, you cannot always tell. Having code like that in the constructor is unhealthy, the window gets created before the constructor completed. It usually comes to a good end but it can make creating the form very slow. Easy to diagnose from the Call Stack window.
  • To avoid a bug in the MDI implementation. If you create an MDI child in the parent constructor then you’ll get duplicated glyphs, visible when you maximize the child. Create the child in OnLoad instead.

Leave a Comment