PRB: VB Form_Load Procedure Not Executed when Unload Not Used

Last reviewed: June 21, 1995
Article ID: Q76629
The information in this article applies to:

- Standard and Professional Editions of Microsoft Visual Basic for

  Windows, versions 2.0 and 3.0
- Microsoft Visual Basic programming system for Windows, version 1.0

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:

  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 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.

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 reference words: 1.00 2.00 3.00
KBCategory: kbprg kbcode kbprb
KBSubcategory: PrgCtrlsStd


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: June 21, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.