Freetutes.com

VB6 beginners tutorial - Learn VB6

Advanced VB6 tutorial - Learn Advanced VB6

Systems Analysis - System analysis and Design tutorial for Software Engineering


You are here: Visual Basic > Advanced VB6 tutorial > Chapter 6

The Load Event and the Activate Event

The Load event fires when the form loads into memory. 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.

A form receives an Activate event when it becomes the active form. A form is the active form in an application when the focus in the application is on the form itself or (more likely) on a control within the form. This could mean that either the form itself has the focus or that a control on the form has focus. The user can tell which form is active because the title bar appears highlighted and the form usually appears on top of the user's desktop, as in Figure 6.1.

An Active form on the user's desktop.
FIGURE 6.1 An Active form on the user's desktop.

NOTE: Forms as an Application's Startup Object As mentioned in Chapter 4, "Creating Data Input Forms and Dialog Boxes," a form might be the application's startup object. In such a case, you can count on the startup form's Initialize, Load, and Activate events to run when the application starts.

Focus can come to the form (and fire the Activate event) either by user action or through program code. For instance, the user can activate the form from another form in the application by clicking on the form with the mouse. Your program can cause a form to become the application's active form by calling that form's Show method or by calling the SetFocus method of one of the form's controls.

The Activate event could therefore fire many more times in an application than either the Load or Initialize events (because it could fire as often as the user returns to the form with the mouse or as often as your application makes it the active form).

Sometimes you will need to decide between putting code in the form's Activate event procedure and putting it in the Load event procedure. The following points can serve as guidelines for deciding between Activate and Load event procedures:

  • The Load event procedure fires before the form is established visually and before the data connections of any data controls that it contains have been established. You will get runtime errors if you place code in form_Load that tries to use data connections belonging to the form's own Data Controls. Code that attempts graphics output to the form in form_Load will have no effect unless you set the form's AutoRedraw property to True.

  • The Activate event can fire multiple times once the form is loaded, so you should be cautious when placing code into the Activate event procedure if you do not want that code to run more than once per form session. If such code must go in the Activate event procedure (say, because it relies on an established data connection), then you could put a Boolean Static variable in the Activate event procedure to keep track of whether the Activate event has already run.


  

<< Previous | Contents | Next >>

Home | About Us | Privacy Policy | Contact Us

Copyright © Freetutes.com | All Rights Reserved