Overflow Error Plotting Points Far Outside Bounds of Control

ID Number: Q81953

1.00

WINDOWS

Summary:

Visual Basic may give an Overflow error when you plot points on a form

or picture box if a point's coordinates far exceed the borders and

scale of the form or control. The point at which overflow occurs

depends on the ScaleMode property value and the points plotted. In the

case of ScaleMode = 0 (User Defined Scale), the size of the form or

picture box and the scale chosen are also determinants.

A workaround is to trap the error and use a RESUME NEXT statement to

exit the error handler. The example below contains the necessary code

to trap the Overflow error.

This information applies to Microsoft Visual Basic programming

system version 1.0 for Windows.

More Information:

Before Visual Basic can plot a point, it must first convert the

coordinates into their absolute location in twips. If, after the

conversion, one or both coordinates are greater than 32,767 or less

than -32,768, an Overflow error is generated. The following chart

lists the ScaleModes, their equivalence in twips, and the values that

will cause a coordinate (z) to overflow:

Equivalents

ScaleMode in Twips (Tp) Overflow Point (z)

--------- ------------- ------------------

0 (User defined) User defined User defined (see example)

1 (Twips) 1 twip = 1 twip (z < -32768) or (z > 32767)

2 (Point) 1 point = 20 twips (z < -1638) or (z > 1638)

3 (Pixel) System dependent System dependent

4 (Character) x-axis=120 twips/char (x < -273) or (x > 273)

y-axis=240 twips/char (y < -136) or (y > 136)

5 (Inch) 1 Inch = 1440 twips (z < -22) or (z > 22)

6 (Millimeter) 1 mm = 56.7 twips (z < -577) or (z > 577)

7 (Centimeter) 1 cm = 567 twips (z < -57) or (z > 57)

The example below can be used to determine the value that generates

the Overflow error for ScaleMode 0 or 3.

Example

-------

1. Run Visual Basic, or from the File menu, choose New Project (ALT,

F, N) if Visual Basic is already running. Form1 is created by

default.

2. Add the following controls to Form1:

Control CtlName

------- -------

Text box Text1

Command button Command1

3. Set the MultiLine property for Text1 to True. With ScaleMode = 0

only, the overflow value is dependent upon the size of the picture

box or form. If you are testing the overflow value with ScaleMode =

0, you must size the form appropriately.

4. Add the following code to the Form1 Form_Load event procedure:

Sub Form_Load ()

Command1.Caption = "Find Ranges"

'* Change ScaleMode to see different results

Form1.ScaleMode = 3 'PIXEL

End Sub

5. Add the following code to GENERAL.BAS:

Const FALSE = 0

Const TRUE = -1

Sub Command1_Click ()

CR$ = Chr$(13) + Chr$(10) 'carriage return

X = FindValue("X")

Y = FindValue("Y")

Text1.Text = "Valid value when..."

Text1.Text = Text1.Text + CR$ + "-" + Str$(X) + " < X < " + Str$(X)

Text1.Text = Text1.Text + CR$ + "-" + Str$(Y) + " < Y < " + Str$(Y)

End Sub

6. Add the following general purpose function to the general

Declarations section:

Function FindValue (Which$)

On Error GoTo rlhandler

HiValue = 100000

LoValue = 0

Errored = FALSE

'do binary select

Do

NewCheck = Value

If Errored Then

Value = HiValue - (HiValue - LoValue) \ 2

Else

Value = LoValue + (HiValue - LoValue) \ 2

End If

If Which$ = "X" Then

Form1.PSet (Value, 0)

Else

Form1.PSet (0, Value)

End If

If ErrorNum = 6 Then

HiValue = Value

ErrorNum = 0

Else

LoValue = Value

End If

Loop Until NewCheck = Value

FindValue = Value

Exit Function

rlhandler:

'Err = 6 is OverFlow error

If Err = 6 Then

ErrorNum = Err

Else

Form1.Print Err

End If

Resume Next

End Function

7. From the Run menu, choose Start (or press F5), and click on

the Command1 button to calculate the point at which the X and Y

coordinates generate an Overflow error.

When the above Click event is triggered, Visual Basic will try to set

a point on the form. Past the border, Visual Basic is plotting points

that exceed the visual scope of the control. Once the program traps

the Overflow error, the text box will display the valid range of

coordinates you can use that will not generate the Overflow error.

Additional reference words: 1.00