This article was previously published under Q138819
The Form_Initialize, Form_Load, and Form_Activate events are in many waysrelated, but also have important and fundamental differences. This articlegives more information and clarifies the relationship among these events.Note that the Form_Initialize event is only present in Visual Basic 4.0;Form_Load and Form_Activate are present earlier versions.
This event fires only when an application first creates an instance of aform. In an application with a startup form, the first event that occurs isthe startup form's Initialize event. Initialize may also be called well inadvance of a Load or Show if, for example, the following statement isexecuted:
Set x = New Form1
In addition, Initialize is called again if all of the references to theform have been previously set to Nothing and the form is then loaded, shownor has a variable set to it (for example, Set x = New Form1).
Many of the tasks previously done in the Form_Load statement can, in VisualBasic 4.0, be performed in the Form_Initialize statement. Any statementthat needs to be performed a single time when a form is created can be donehere. Be careful that you do not reference any of the properties of thecontrols on the form because doing so will cause an automatic Form_Load.
The Form_Load event fires when a form is first loaded into memory. A formcan be caused to load by an explicit Load statement, by calling a Showmethod when the form is not already loaded, or by a line of code in anothermodule making a reference to any of the unloaded form's properties (or toany properties of the unloaded form's children).
For example, if Form2 is not loaded, and a subroutine in Form1 executes aline of code setting or reading Form2's Caption property, then Form2 willbe loaded into memory, and the corresponding Form2_Load event will becalled. Unless a form is loaded as a result of a Show method or has itsVisible property set to True, the form will be loaded into memory but willnot be made visible. Forms can be hidden and shown (using the Hide and Showmethods) while remaining in memory; the Load event will only be fired whenthe form is first loaded.
Some procedures and methods of controls (for example, the SetFocus method)require that their parent form be loaded before they are called. In thissituation, the best thing to do is to perform an explicit Form.Show in theForm_Load event, and do any such initialization after calling the Showmethod. This is preferable to doing the initialization in, for example, theForm_Activate event because Activate is called every time the user switchesbetween forms in an application. Also, if you are calling related events,such as Activate, Paint, or Resize, be careful that their actions do notconflict with the Load and that they don't fire recursive events.
As was stated previously, the Activate event is called every time a formbecomes the active window, as long as the focus has moved between forms inthe application. The Activate event is not called if the previous windowthat had the focus was outside of the application.
There are fewer caveats with Activate than with the previous two eventprocedures, perhaps because Activate is marginally less important than theInitialize and Load events. Code that needs to be executed every time thefocus changes within an application should reside in the form's activateevent.
For more information, please search the Visual Basic 4.0 Help file orprinted documentation for the Initialize, Load, and Activate events.