VB Procedure Form_Load Not Executed when Unload Not Used

ID Number: Q76629

1.00

WINDOWS

Summary:

Code inside a Form_Load event procedure will not execute under the

circumstances described below. The example below helps clarify the

behavior of the Load event procedure.

A Load event procedure will only execute when a form is loaded, either

with the Load statement or an implicit load. An implicit load is

caused when a form is currently not loaded, and a property or method

accesses the form or associated control.

This behavior is by design in Microsoft Visual Basic programming

system version 1.0 for Windows.

More Information:

Below is a demonstration of this behavior:

1. From the File menu, choose New Project.

2. From the File menu, choose New Form.

3. Place a command button on each form. Place command button 1 on form

1 and command button 2 on form 2.

4. Place the following code in the event procedure Command1_Click

in form 1:

Sub Command1_Click ()

Form1.MousePointer = 11 'Hourglass pointer

Form2.Show

End Sub

5. Add the following code in the event procedure Form_Load in form 1:

Sub Form_Load ()

Form1.MousePointer = 0 'Default pointer

End Sub

6. Add the following code in the event procedure Command2_Click in

form 2:

Sub Command2_Click ()

Form2.MousePointer = 11 'Hourglass pointer

Form1.Show

End Sub

7. Add the following code in the event procedure Form_Load in form 2:

Sub Form_Load ()

Form2.MousePointer = 0 'Default pointer

End Sub

8. Run the program with the F5 key. You will see Form1 load up

with the Command1 button on it. If you click on the Command1 button,

you will see the mouse cursor change to an hourglass until Form2 is

loaded. With Form2 loaded, you can see that the mouse cursor is back

to the default arrow. Click on the Command2 button and see the mouse

cursor change back to an hourglass until Form1 is loaded.

This is where the behavior starts; the hourglass continues

to be displayed instead of going back to the default arrow. This

is because the code Form1.MousePointer = 0 in the Form_Load

event procedure of Form1 is not being executed. You can continue

by clicking on Command1 button again to go to Form2 and the

hourglass continues to be displayed.

The easiest way to work around this behavior is to add an Unload

statement after each .Show statement, as shown below:

Sub Command1_Click ()

Form1.MousePointer = 11

Form2.Show

Unload Form1 'new line of code to be added

End Sub

Sub Command2_Click ()

Form2.MousePointer = 0

Form1.Show

Unload Form2 'new line of code to be added

End Sub

Note: This method may slow the painting of forms at run-time, but this

method will guarantee that the Form_Load event procedure is executed

when the Show method is executed.

Another workaround is to place the code

.MousePointer = 0 statements

into the Form_Paint event procedures. Note that this method will only

work when one form is being painted over another. Use the Cut and

Paste routines from the Edit menu of Visual Basic. Cut the following

line of code

Form1.MousePointer = 0

from the event procedure Form_Load in Form1 and paste the code into

the Form1 Form_Paint event procedure. Repeat the same Cut and Paste

task in Form2, placing the code

Form2.MousePointer = 0

in the Form2 Form_Paint event procedure.

Additional reference words: 1.00