## Visual Basic in 12 Easy Lessons velp11.htm

- Stop & Type
  - The Program's Description
  - Descriptions
  - Close the Application

#
Project 11

##
Spruce Up your Programs

###
Stop & Type

This lesson taught you how to use the printer for text reports and use the screen for fancy graphics. Although business and scientific data processing requires printed reports, this unit's real glamour appeared in Unit 22 when you learned all about the line and shape controls.

The visual in Visual Basic becomes most apparent when you add graphics that capture the user's eye. Perhaps the control that provides more different formats than any other is the shape control, which enables you to draw squares, circles, and ovals of all shapes, sizes, and colors. This unit takes the shape control to the max by demonstrating a fancy form that blows up in your face with more than 150 sets of colored circles, squares, and ovals.

In this lesson, you saw the following:

- When to access the Printer object
- How to use the Print method to produce text on the printed page
- What shapes are available from the shape control
- How to draw lines on the form using the line control
- When to use the picture box and image controls to place file graphics on a form

####
The Program's Description

Figure P11.1 shows the PROJEC11.MAK opening Form window. Wow! When you load and run the program, the program generates all kinds of circles on the form. The entire collection of circles, as well as the subsequent graphic collections that appear after the circles, all are different owing to the program's use of the Rnd() function introduced in project 10.

Figure P11.1. Project 11 runs circles around other applications.

As you click the Next Shape command button, the program replaces the circles with a similar mosaic of squares. Pressing Next Shape again produces a series of ovals. You can continue cycling through the circles, squares, and ovals as often as you want until you press the Exit command button to terminate the program.

The program contains a control array with 160 shape controls named shpShape. Figure P11.2 shows the form at design time. The location of the shape controls at design time is irrelevant to the program because the code relies heavily on the Rnd() function to display the shapes in random screen locations, with random colors, sizes, and fill patterns.

Figure P11.2. project 11's design mode shows an interesting placement of a control array containing shapes.

The program depends so much on the Rnd() function that a special function subroutine is included that converts Rnd() into a random number, from 1 to whatever argument is passed to the function. The function procedure's name is RndNum(); you'll see the function in the code description that appears in Listing P11.1.

Listing P11.1. The code that generates all the graphics on the form.

    1: Option Explicit

    2:

    3: Sub Form_Activate ()

    4: ' Get
    things started by displaying circles

    5: Call Circles

    6: End Sub

    7:

    8: Sub Circles ()

    9: ' Draws several circles of different

    10: ' colors and sizes on the form

    11: Dim Count As Integer

    12: lblShape.Caption = "Circles"

    13: For Count = 0
    To 159 ' 160 total shapes

    14: shpShape(Count).Shape = SHAPE_CIRCLE

    15: shpShape(Count).Visible = False

    16: shpShape(Count).Top = RndNum(3000)

    17: shpShape(Count).Width = RndNum(5000)

    18: shpShape(Count).Height = RndNum(3000)

    19: shpShape(Count).Left
    = RndNum(3000)

    20: shpShape(Count).BackColor = QBColor(RndNum(16) - 1) ' Adjust for zero

    21: shpShape(Count).FillColor = QBColor(RndNum(16) - 1) ' Adjust for zero

    22: shpShape(Count).Visible = True

    23: shpShape(Count).FillStyle = RndNum(8) - 1 ' Adjust
    for zero

    24: Next Count

    25: End Sub

    26:

    27: Sub Ovals ()

    28: ' Draws several ovals of different

    29: ' colors and sizes on the form

    30: Dim Count As Integer

    31: lblShape.Caption = "Ovals"

    32: For Count = 0 To 159 ' 160 total shapes

    33:
    shpShape(Count).Shape = SHAPE_OVAL

    34: shpShape(Count).Visible = False

    35: shpShape(Count).Top = RndNum(3000)

    36: shpShape(Count).Width = RndNum(4000)

    37: shpShape(Count).Height = RndNum(3000)

    38: shpShape(Count).Left = RndNum(3000)

    39:
    shpShape(Count).BackColor = QBColor(RndNum(16) - 1) ' Adjust for zero

    40: shpShape(Count).FillColor = QBColor(RndNum(16) - 1) ' Adjust for zero

    41: shpShape(Count).Visible = True

    42: shpShape(Count).FillStyle = RndNum(8) - 1 ' Adjust for zero

    43: Next
    Count

    44: End Sub

    45:

    46: Sub Squares ()

    47: ' Draws several squares of different

    48: ' colors and sizes on the form

    49: Dim Count As Integer

    50: lblShape.Caption = "Squares"

    51: For Count = 0 To 159 ' 160 total shapes

    52:
    shpShape(Count).Shape = SHAPE_SQUARE

    53: shpShape(Count).Visible = False

    54: shpShape(Count).Top = RndNum(3000)

    55: shpShape(Count).Width = RndNum(5000)

    56: shpShape(Count).Height = RndNum(3000)

    57: shpShape(Count).Left = RndNum(3000)

    58:
    shpShape(Count).BackColor = QBColor(RndNum(16) - 1) ' Adjust for zero

    59: shpShape(Count).FillColor = QBColor(RndNum(16) - 1) ' Adjust for zero

    60: shpShape(Count).Visible = True

    61: shpShape(Count).FillStyle = RndNum(8) - 1 ' Adjust for zero

    62: Next
    Count

    63: End Sub

    64:

    65: Sub cmdShape_Click ()

    66: ' Check the caption of the description

    67: ' label to see what shapes to draw next

    68: Select Case Left$(lblShape.Caption, 1)

    69: Case "C":

    70: Call Squares

    71: Case "S"

    72:
    Call Ovals

    73: Case "O":

    74: Call Circles

    75: End Select

    76: End Sub

    77:

    78: Sub cmdExit_Click ()

    79: End

    80: End Sub

    81:

    82: Function RndNum (n)

    83: ' Returns a random number from 1 to n

    84: RndNum = Int(n * Rnd + 1)

    85: End
    Function

####
Descriptions

1: Requires that all variables be defined before their use.

2: A blank line that helps separate parts of the program.

3: The code for the event procedure that executes as soon as the user sees the form for the first time.

4: A remark that explains the procedure.

> > 4: Always remember to generate an initial form.

5: Execute the subroutine procedure that draws the circles.

6: Terminate the procedure.

7: A blank line separates procedures.

8: The subroutine procedure that draws circles.

9: A remark that explains the procedure.

10: The remark continues.

11: Define a variable that will control the loop.

12: Set the caption on the description label.

13: Step through all 160 elements of the control array.

> > 13: There are 160 elements in the shape control array to display.

14: Set the shape of each control to a circle.

15: Hide the control, temporarily, until the properties are set.

16: Set an appropriate placement from the top of the form.

17: Set an appropriate width.

18: Set an appropriate height.

19: Set an appropriate placement from the left edge of the form.

20: Set the background color by calling the built-in QBColor() function and passing a value from 0 to 15.

21: Set the fill color by calling the built-in QBColor() function and passing a value from 0 to 15.

22: Display the control now that the size and placement properties are set.

23: Fill the shape with a fill number from 0 to 8.

24: Prepare for the next shape in the control array.

25: Terminate the procedure.

26: A blank line separates the procedures.

27: The subroutine procedure that draws ovals.

28: A remark that explains the procedure.

29: The remark continues.

30: Define a variable that will control the loop.

31: Set the caption on the description label.

32: Step through all 160 elements of the control array.

33: Set the shape of each control to a oval.

34: Hide the control, temporarily, until the properties are set.

35: Set an appropriate placement from the top of the form.

36: Set an appropriate width.

37: Set an appropriate height.

38: Set an appropriate placement from the left edge of the form.

39: Set the background color by calling the built-in QBColor() function and passing a value from 0 to 15.

> > 39: The QBColor() function accepts a value from 0 to 15 that represents a different color.

40: Set the fill color by calling the built-in QBColor() function and passing a value from 0 to 15.

41: Display the control now that the size and placement properties are set.

42: Fill the shape with a fill number from 0 to 8.

43: Prepare for the next shape in the control array.

44: Terminate the procedure.

45: A blank line separates the procedures.

46: The subroutine procedure that draws squares.

47: A remark that explains the procedure.

48: The remark continues.

49: Define a variable that will control the loop.

50: Set the caption on the description label.

51: Step through all 160 elements of the control array.

52: Set the shape of each control to a square.

> > 52: The named constants in CONSTANT.TXT eliminate the need to remember numeric property equivalents.

53: Hide the control, temporarily, until the properties are set.

54: Set an appropriate placement from the top of the form.

55: Set an appropriate width.

56: Set an appropriate height.

57: Set an appropriate placement from the left edge of the form.

58: Set the background color by calling the built-in QBColor() function and passing a value from 0 to 15.

59: Set the fill color by calling the built-in QBColor() function and passing a value from 0 to 15.

60: Display the control now that the size and placement properties are set.

61: Fill the shape with a fill number from 0 to 8.

62: Prepare for the next shape in the control array.

63: Terminate the procedure.

64: A blank line separates the procedures.

65: A procedure that executes to display the next set of shapes when the user clicks the cmdShape command button.

66: A remark that explains the procedure.

67: The remark continues.

68: Determine the next shape by looking at the label that describes the previously displayed shape.

> > 68: The code looks at the first character of the label to decide what shape to display next.

69: If the label contains Circles...

70: Change the display to random squares.

71: If the label contains Squares...

72: Change the display to random ovals.

73: If the label contains Ovals...

74: Change the display to random circles.

75: Terminate the Select statement.

76: Terminate the procedure.

77: A blank line separates the procedures.

78: A procedure that ends the program when the user clicks the cmdExit command button.

79: Terminate the program's execution.

80: Terminate the procedure.

81: A blank line separates the procedures.

82: A function procedure that accepts an integer argument and that returns a random number from 1 to the argument.

83: A remark explains the function.

84: Set the function's return value to the generated random number.

> > 84: A function procedure keeps you from having to repeat the same code throughout the program.

85: Terminate the procedure.

####
Close the Application

You now can exit the application and exit Visual Basic. The next lesson teaches you how to use the remaining controls and Visual Basic's online debugging tool to help locate and correct errors within code.