## Visual Basic in 12 Easy Lessons velp12.htm

- Stop & Type
  - The Program's Description
  - Studying the Grid
  - Descriptions
  - Using the Command Buttons and Scroll Bars
  - Description
  - Close the Application

#
Project 12

##
Getting Exotic

###
Stop & Type

This lesson taught you how to use the scroll bars and the grid control, to manage the mouse, and to use Visual Basic's integrated debugger. The scroll bars give users a flexible control for entering and selecting from a range of data values. The grid enables the user to see tables of information in a spreadsheet-like format. When you need to monitor the user's mouse movements, in addition to the built-in monitoring that Visual Basic performs for all control clicking with the mouse, you can write code that responds to the user's mouse movements and clicks.

The debugger is an integrated development tool that helps you hunt down errors that appear in your code. The debugger allows you to single step through a program's code, monitor variables, and inquire into a program's data values at any point in the running of the program.

In this lesson, you saw the following:

- How to place and monitor scroll bar controls
- How to set up a grid for tables of data
- How to determine selected cells within a grid
- How to monitor the user's mouse movements
- How to use Visual Basic's integrated debugger to remove obscure bugs in your programs

####
The Program's Description

Figure P12.1 shows the PROJEC12.MAK opening Form window. Much of this project comes from a description of a scroll bar application found in the first unit of this lesson. As Figure P12.1 shows, the user first sees a formatted table of values stored in a grid.

Figure P12.1. Project 12's application begins with a simple form.

> > Warning: You must make sure that the GRID.VBX customer control file for the grid control appears in your Visual Basic's directory. See Unit 23 for details on adding this file.

The application demonstrates how to use, change, and monitor the user's use of a grid control as well as how you can use scroll bars to change the behavior of other controls on the form.

####
Studying the Grid

The grid represents the lawn fertilizer's application price table for the 8 customer's 5 lawn applications. As described in Unit 23, this lawn care company just began and needs to make 40 service calls that year. As the company adds more customers, the grid can easily be expanded by changing the grid's property values and adding the new values in the code. Once the number of customers grew by just a few more, the lawn service would want to incorporate the disk drive and store the current pricing table on the disk drive for easy maintenance and for use by other programs such as an invoicing program.

Try this: Click the grid's scroll bars to scroll around the pricing table looking at the 40 prices. Before worrying about the command buttons and horizontal scroll bars at the right of the screen, study listing P12.1 for a glimpse of the overall program's format.

Listing P12.1. The Form_Load() procedure that controls the flow of the initial code.

    1:
    Sub Form_Load ()

    2: ' Defines the justification of the cells

    3: ' as well as assigns cell titles to the

    4: ' fixed row and columns and assigns initial

    5: ' price values to the 40 cells.

    6: Call InitScrolls ' Initialize scroll bars

    7: Call CenterCells
    ' Center all cell alignments

    8: Call SizeCells ' Specify width of cells

    9: Call Titles ' Initialize column and row titles

    10: Call FillCells ' Fill cells with values

    11: End Sub

> > 6: Break long modules into shorter ones.

> > Definition: Structured programs are programs broken into several small sections of code.

Analysis: The code in Listing P12.1 demonstrates a programming approach called structured programming that you should incorporate into your own programs. Form_Load() isn't one huge event procedure because the Call statements in lines 6 through 10 help break up the code into smaller and more manageable code routines. All of the called code executes when the Form_Load() event procedure runs (right before the user sees the form on the screen), but the five subroutines provide a way for you to break the program into sections that let you zero in later on parts that you need to modify.

Suppose that you need to update the pricing information. If you study only the Form_Load() procedure, you can see that the FillCells subroutine fills the grid's cells with values. Therefore, you could go straight to that subroutine if you needed to change the prices.

Listing P12.2 contains the code for the routines called by the Form_Load() procedure. Study the code to familiarize yourself with your own grid's initialization requirements.

Warning: Not all of the code for the FillCells and Titles subroutines is listed in Listing P12.2 because the code is lengthy and repetitive. The long string of assignment statements helps to show how loading and saving the pricing information from and to the disk would be helpful once the company added many more customers. You don't want to look through and modify long strings of assignments every time that a price needs updating, you get a new customer, or you lose a customer. Using the disk for pricing, however, would probably require at least one additional program that enables you to update the disk price information directly.

Listing P12.2. The code that the Form_Load() procedure calls to initialize the grid and other controls.

    1: Sub InitScrolls ()

    2: ' Set both scroll bars to their maximum values

    3: hscIncrease.Value = 15

    4: hscDecrease.Value = 15

    5: End Sub

    6:

    7: Sub CenterCells ()

    8: ' Sets the justification of the grid's

    9: ' cells to center alignment

    10: Dim Apps As Integer ' 5 applications yearly

    11: ' Center all values in the grid

    12: For Apps = 0 To 5 ' 5 applications

    13: grdLawn.Col = Apps ' Locate next column

    14: ' Center the fixed cells in this column

    15: grdLawn.FixedAlignment(Apps) = 2

    16: Next Apps

    17: For Apps = 1 To 5 ' 5 applications

    18: ' Center the non-fixed cells in this column

    19: grdLawn.ColAlignment(Apps) = 2

    20: Next Apps

    21: End Sub

    22:

    23: Sub SizeCells ()

    24: ' Specify the width of each cell

    25: Dim Apps As Integer
    ' 5 Application

    26: For Apps = 0 To 5

    27: grdLawn.ColWidth(Apps) = 1100 ' Twips

    28: Next Apps

    29: End Sub

    30:

    31: Sub Titles ()

    32: ' Fill in the column titles

    33: grdLawn.Row = 0 ' All titles are in row #0

    34: grdLawn.Col = 1

    35: grdLawn.Text =
    "Pre-Emerge"

    36: grdLawn.Col = 2

    37: grdLawn.Text = "Deep"

    38: ' Rest of column titles are filled here

    39: ' Fill in the row titles

    40: grdLawn.Col = 0 ' Customer titles in first column

    41: grdLawn.Row = 1

    42: grdLawn.Text =
    "Jones"

    43: grdLawn.Row = 2

    44: grdLawn.Text = "Smith"

    45: ' Rest of row titles are filled here

    46: End Sub

    47:

    48: Sub FillCells ()

    49: ' Fill in all 40 cells one at a time.

    50: ' This is tedious!

    51: '

    52: ' Normally, you
    would fill these cells from

    53: ' a disk file or through calculations.

    54: grdLawn.Row = 1

    55: grdLawn.Col = 1

    56: grdLawn.Text = 43.16

    57: grdLawn.Col = 2

    58: grdLawn.Text = 41.56

    59: grdLawn.Col = 3

    60: grdLawn.Text = 34.57

    61: grdLawn.Col = 4

    62: grdLawn.Text = 27.49

    63: grdLawn.Col = 5

    64: grdLawn.Text = 41.34

    65: grdLawn.Row = 2

    66: grdLawn.Col = 1

    67: grdLawn.Text = 43.56

    68: ' Rest of cells filled here

    69: End Sub

####
Descriptions

1: The code for the subroutine procedure that sets the scroll bars to their initial maximum values.

2: A remark that explains the procedure.

3: Set the Increase scroll bar to its maximum value of 15, which represents a 15 percent increase when the user clicks the Increase command button.

4: Set the Decrease scroll bar to its maximum value of 15, which represents a 15 percent decrease when the user clicks the Decrease command button.

5: Terminate the subroutine.

6: A blank line to separate the procedures.

7: The code for the subroutine procedure that sets the alignment in all the grid's cells to centered justification.

8: A remark explains the procedure.

9: The remark continues.

10: Define a variable used to loop through the number of applications.

11: A remark explains the code.

12: Step through all of the table's six columns (the first column, column 0, holds the row titles).

13: Set the column number to the For loop's value.

14: A remark explains the code.

15: Center (the value of 2 sets the property to 2-Center) the alignment property of all the fixed rows in the selected column. Only the top row is fixed in the last five columns.

> > 15: Visual Basic right-justifies cell values if you don't change the justification.

16: Continue the loop.

17: Step through the table's last five columns (these contain the nonfixed columns) to prepare for centering.

18: A remark explains the code.

19: Center (the value of 2 sets the property to 2-Center) the alignment property of all the nonfixed rows in the selected column.

20: Continue the loop.

21: Terminate the subroutine.

22: A blank line to separate the procedures.

23: The code for the subroutine procedure that sets the width of the cells.

24: A remark explains the procedure.

25: Define a variable used to loop through the number of applications.

26: Step through each column, including the first fixed column 0.

27: Set every cell to 1,100 twips wide.

> > 27: Make sure that each cell is wide enough to hold all data and titles.

28: Continue the loop.

29: Terminate the subroutine.

30: A blank line to separate the procedures.

31: The code for the subroutine procedure that initializes all the titles in the grid's fixed rows and columns.

32: A remark explains the procedure.

33: Set up for the first row (row number 0).

34: Set up for the second column (column number 1).

35: Assign a title.

36: Move to the next column in the same row.

37: Assign a title.

38: A remark to explain that not all the code is shown.

39: A remark explains the code.

40: Set up for the first column (column number 0).

41: Set up for the second row (row number 1).

42: Assign a title.

43: Move to the next row in the same column.

44: Assign a title.

45: A remark to explain that not all the code is shown.

46: Terminates the subroutine.

47: A blank line to separate the procedures.

48: The code for the subroutine procedure that initializes all the values in the grid's nonfixed cells.

49: A remark explains the procedure.

50: The remark continues.

51: The remark continues.

52: The remark continues.

53: The remark continues.

54: Set up for the second row (the first nonfixed row).

> > 54: The Row and Col properties determine which cell gets the next value.

55: Set up for the second column (the first nonfixed column).

56: Assign a price value.

57: Move to the next column.

58: Assign a price value.

59: Move to the next column.

60: Assign a price value.

61: Move to the next column.

62: Assign a price value.

63: Move to the next column.

64: Assign a price value.

65: Move to the next row.

66: Move to the next column.

67: Assign a price value.

68: A remark to explain that not all code is listed here.

69: Terminate the subroutine.

####
Using the Command Buttons and Scroll Bars

The application is set up to allow the user to select one or more cells with the mouse and then update the price values for only those selected cells. The user can thus try various pricing scenarios to see which pricing structure works best for specific applications. When the user clicks either command button, the selected cell or cells all increase or decrease by the amount shown on the command button at the time of the click. All unselected cells remain unaffected by the price change.

The scroll bars affect the price of the change as well as the captions on the command buttons themselves. For example, if the user lowers the value of the Decrease scroll bar, the Decrease by... command button's caption changes accordingly. The next time the user clicks the command button, the selected cells will decrease in price by the amount indicated on the scroll bar. The code in Listing P12.3 describes the interaction between the two scroll bars and their appropriate command buttons.

Listing P12.3. Managing the price changes through scroll bars, command buttons, and selected cells.

    1: Sub hscDecrease_Change ()

    2: ' Change the command button's caption

    3: cmdDecrease.Caption = "&Decrease by" & Str$(hscDecrease.Value) & "%"

    4:
    End Sub

    5:

    6: Sub hscIncrease_Change ()

    7: ' Change the command button's caption

    8: cmdIncrease.Caption = "&Increase by" & Str$(hscIncrease.Value) & "%"

    9: End Sub

    10:

    11: Sub cmdDecrease_Click ()

    12: ' Decrease
    selected cell values

    13: ' by the decreasing scroll bar percentage

    14: Dim SelRows, SelCols As Integer

    15: If (grdLawn.HighLight) Then ' If true...

    16: For SelRows = grdLawn.SelStartRow To grdLawn.SelEndRow

    17: For SelCols = grdLawn.SelStartCol To
    grdLawn.SelEndCol

    18: grdLawn.Row = SelRows

    19: grdLawn.Col = SelCols

    20: ' Decrease the cell by scroll bar amount

    21: grdLawn.Text = grdLawn.Text - (hscDecrease.Value / 100 * grdLawn.Text)

    22: grdLawn.Text = Format(grdLawn.Text, "Fixed")

    23: Next SelCols

    24: Next SelRows

    25: End If

    26: End Sub

    27:

    28: Sub cmdIncrease_Click ()

    29: ' Increase selected cell values

    30: ' by the amount of the increase scroll bar's percentage

    31: Dim SelRows, SelCols As Integer

    32: If
    (grdLawn.HighLight) Then ' If true...

    33: For SelRows = grdLawn.SelStartRow To grdLawn.SelEndRow

    34: For SelCols = grdLawn.SelStartCol To grdLawn.SelEndCol

    35: grdLawn.Row = SelRows

    36: grdLawn.Col = SelCols

    37: ' Increase the cell by scroll bar
    amount

    38: grdLawn.Text = grdLawn.Text * (1 + hscIncrease.Value / 100)

    39: grdLawn.Text = Format(grdLawn.Text, "Fixed")

    40: Next SelCols

    41: Next SelRows

    42: End If

    43: End Sub

    44:

    45: Sub cmdExit_Click ()

    46: End

    47: End
    Sub

####
Description

1: The code for the event procedure that executes when the user changes the decreasing scroll bar.

2: A remark explains the procedure.

3: Change the command button's caption to reflect the new percentage decrease.

> > 3: One event procedure can change the property values of another.

4: Terminate the procedure.

5: A blank line separates the procedures.

6: The code for the event procedure that executes when the user changes the increasing scroll bar.

7: A remark explains the procedure.

8: Change the command button's caption to reflect the new percentage increase.

9: Terminate the procedure.

10: A blank line separates the procedures.

11: The code for the event procedure that executes when the user clicks the Decrease command button.

12: A remark that explains the event procedure.

13: The remark continues.

14: Define two variables that control the selected cells.

15: If there are highlighted cells...

> > 15: There are no selected cells if no cells are highlighted.

16: Step through the selected rows.

17: Step through the selected columns.

18: Set the row to be changed.

19: Set the column to be changed.

20: A remark explains the code.

21: Update the cell's value by the amount of the decrease.

22: Format the cell to two decimal places.

23: Continue stepping through the selected columns.

24: Continue stepping through the selected rows.

25: Terminate the If statement.

26: Terminate the procedure.

27: A blank line separates the procedures.

28: The code for the event procedure that executes when the user clicks the Increase command button.

29: A remark that explains the event procedure.

30: The remark continues.

31: Defines two variables that control the selected cells.

32: If there are highlighted cells...

33: Step through the selected rows.

34: Step through the selected columns.

35: Set the row to be changed.

36: Set the column to be changed.

37: A remark explains the code.

38: Update the cell's value by the amount of the increase.

> > 38: Does not affect any cells that are unselected.

39: Format the cell to two decimal places.

40: Continue stepping through the selected columns.

41: Continue stepping through the selected rows.

42: Terminate the If statement.

43: Terminate the procedure.

44: A blank line separates the procedures.

45: The code for the event procedure that executes when the user clicks the Exit command button.

46: Terminate the program.

47: Terminate the procedure.

####
Close the Application

You can now exit the application and exit Visual Basic. Congratulations! You are now a Visual Basic programmer!