Why Output Might Not Display from VB Form_Load Procedure

Last reviewed: June 21, 1995
Article ID: Q71101
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

SUMMARY

Any graphics or output done within a Form_Load procedure will not display on the form unless you first make the form visible with the Form1.Show method or if you set the form's AutoRedraw property to be true (non-zero).

MORE INFORMATION

When the Form_Load procedure executes (at the beginning of the program), by default the form is not yet displayed. Therefore, during the Form_Load event, no graphics are displayed to the nonexistent form unless you first Show the form (at run time) or set the form's AutoRedraw property (at design time or run time).

A better approach to drawing graphics to the form is to have the graphics drawn to the form during a Sub Form_Paint procedure. This allows the Form.AutoRedraw property to be set to FALSE, increasing the speed performance of your program. Visual Basic does not have to refresh the screen image of your form as it does when a form is overlapped with another window. You (as the programmer) are responsible for refreshing the form, and Sub Form_Paint is the most logical place to handle this situation.

Listed below are three examples of drawing graphics to your form. The first example shows how the graphics fail to be displayed to the form when drawn from within a Form_Load event procedure. The second example shows how you could draw a circle to the form, but the Form.AutoRedraw property must be set to TRUE for the circle to be retained in the event the form needs to be refreshed. The third example is the best approach; it is the fastest and most efficient of the three.

For each example below, add the following Function procedure as a code procedure to Form1.

Function Minimum! (n1!, n2!)

    If n1! < n2! Then
        Minimum! = n1!
    Else
        Minimum! = n2!
    End If
End Function

Example 1

No graphic is displayed to the form in the following:

Sub Form_Load
   Row = Form1.ScaleHeight / 2
   Col = Form1.ScaleWidth / 2
   Radius = Minimum(Row, Col)   ' Function that returns smaller number.
   Form1.Circle (Col, Row), Radius
End Sub

Example 2

This example will work, but the AutoRedraw property of Form1 must be TRUE for the screen to refresh properly:

Sub Form_Load
   Form1.Show
   Form1.AutoRedraw = -1
   Row = Form1.ScaleHeight / 2
   Col = Form1.ScaleWidth / 2
   Radius = Minimum(Row, Col)   ' Function that returns smaller number.
   Form1.Circle (Col, Row), Radius
End Sub

Example 3

This is the best example. AutoRedraw should be set to FALSE for better speed and efficiency.

Sub Form_Paint
   Row = Form1.ScaleHeight / 2
   Col = Form1.ScaleWidth / 2
   Radius = Minimum(Row, Col)   ' Function that returns smaller number.
   Form1.Circle (Col, Row), Radius
End Sub


Additional reference words: 1.00 2.00 3.00
KBCategory: kbprg kbcode
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.