Why Output Might Not Display from VB Form_Load Procedure

ID Number: Q71101

1.00

WINDOWS

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

This information applies to Microsoft Visual Basic Programming System

version 1.0 for Windows.

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