Wednesday, December 12, 2012

Initialize, Load, and Activate Events


Initialize Event
The Initialize event fires when an instance of the form is created in your application. Initialize fires just before Load the very first time the form is loaded into memory during the application's current session.
Load Event
The Load event fires when the form loads into memory. If the form is loaded, unloaded, and re-loaded into memory several times in the application, then the form's Load event will fire at each load. This event's procedure is the customary place for programmers to insert code that sets form-level Private variables (not associated with custom properties) and performs other startup processes.
Activate event
The Activate event fires most often (at least on a multi-form application) because the user can navigate back and forth between forms many times after the forms are loaded in memory and visible.
DeActivate, Unload, QueryUnload, and Terminate Events
These events fire toward the end of a form's life. Their event procedures are therefore good places to put "cleanup" code. Once again, the exact circumstances and timing of these events can differ.
DeActivate Event
The DeActivate event fires when a form or a control on the form loses focus to another object in the application outside the form. The DeActivate event does not fire when the user navigates to a completely different application, but only when the user navigates elsewhere in the current application. The DeActivate event does not fire when the application closes or when the current form unloads.
QueryUnload Event
The QueryUnload event fires just before a form unloads from memory. The QueryUnload event fires just before the Unload event. Its main purpose is to let you detect why the form is being unloaded and to programmatically halt unloading if necessary.
Unload Event
The Unload event procedure is where programmers usually put cleanup code. The Unload event fires after the QueryUnload event. It is possible to stop the form from unloading in the Unload event procedure by setting Cancel to True.
Terminate Event
The Terminate event happens when the instance of the form has been completely unloaded and all the form's variables have been set to Nothing. The Terminate event happens only when you explicitly set the form to Nothing in your code during or after the Unload event procedure.