Microsoft KB Archive/76629

= PRB: VB Form_Load Procedure Not Executed when Unload Not Used =

Article ID: 76629

Article Last Modified on 12/12/2003

-

APPLIES TO


 * Microsoft Visual Basic 2.0 Standard Edition
 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 2.0 Professional Edition
 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 1.0 Standard Edition

-



This article was previously published under Q76629



SYMPTOMS
Code inside a Form_Load event procedure will not execute under the circumstances described in this article. The example below helps clarify the behavior of the Load event procedure.



CAUSE
A Load event procedure executes only 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.



STATUS
This behavior is by design.



MORE INFORMATION
Below is a demonstration of this behavior:

 From the File menu, choose New Project. From the File menu, choose New Form. Place a command button on each form. Place command button 1 on form 1 and command button 2 on form 2.  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   Add the following code in the event procedure Form_Load in form 1: Sub Form_Load Form1.MousePointer = 0  'Default pointer End Sub   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 </li>  Add the following code in the event procedure Form_Load in form 2: Sub Form_Load Form2.MousePointer = 0  'Default pointer End Sub </li> Run the program with the F5 key. You will see Form1 load up with the Command1 button on it. If you click 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 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 the Command1 button again to go to Form2 and the hourglass continues to be displayed.</li></ol>

Workaround
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 this code: .MousePointer = 0 statements into the Form_Paint event procedures. Note that this method will work only when one form is being painted over another. Use the Cut and Paste routines from the Edit menu of Visual Basic. Cut this 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 query words: 2.00 3.00

Keywords: kbprb KB76629

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.