Saturday, December 26, 2009

Determine entry with Sub Main()

Determining Your Entry Point with Sub Main()


By default, when you start a Visual Basic project, the first form created will be the first form that the project loads. This is adequate if you have a one-form project. What if you have a project with many
forms or a project with no forms at all? For the multiple-form problem, you could "chain" Form_Load() event procedures--have one form's Load event procedure Load another form--as follows:

Private Sub Form_Load()
Load frmAnotherForm
End Sub

This would work adequately for projects with a limited amount of forms, but it's not the best programming practice, particularly if you have projects that require the presentation of many forms.

For projects that have no forms (and there are such projects, particularly in server-side Internet programs that use Visual Basic), there's nothing to load. What do you do for an entry point (or starting
point) for your program? Visual Basic provides a nonform-based entry point for your program--the Sub Main() procedure. Sub Main() is a special procedure reserved by Visual Basic as the startup
procedure for any given project. Sub Main() must be declared in a module, and there can be only one Sub Main() per project.

Set Sub Main() to be the startup point for a project
1. From the Project menu, choose ProjectName Properties.

2. Select Sub Main from the Startup Object drop-down list on the General page of the Project Properties dialog (see Figure 18.5).

3. Click OK.


FIGURE 18.5 You can choose Sub Main() or any form in your project as the startup object.

After you define Sub Main() to be the startup object for your project, you need to create Sub Main in a module. You can use the Add Procedure dialog that you've used to create user-defined procedures,
or you can manually enter the declaration in the General section of your chosen module. Remember, a project can have only one Sub Main(). After you create Sub Main(), you need to fill in some startup code.

Listing 3.8 shows a Sub Main() that displays two forms by using the Show method (lines 4 and 5) and then displays a message box after all the forms are visible (line 8).


LISTING 3.8 18LIST07.TXT--A Simple Sub Main()

01 Sub Main()
02 `Use the Show method to display both
03 `forms upon startup
04 frmMain.Show
05 frmOther.Show
06
07 `Report that all forms are shown
08 MsgBox "Everything shown"
09 End Sub

The code for some versions of Sub Main() can be simple, but some Sub Main() procedures can be complex. Listing 3.9 shows how Sub Main() is used to invoke a comprehensive startup routine that calls other procedures.

LISTING 3.9 18LIST09.TXT--A More Complex Sub Main()

01 Sub Main()
02 `Load the form and let it run the code in the
03 `Form_Load event handler
04 Load frmMain
05 `Intialize the contact list combo box
06 Call InitComboAsDb(frmMain.cboName, frmMain.DataMain)
07 `Fill the appointment list with today's appoinments
08 Call GetDailyAppointments(CDbl(Cdate _
(frmMain.FormDateString())), _
frmMain.lstSchedule, _
frmMain.DataMain, _
gAppointmentDelta%)
09 `Show the main form
10 frmMain.Show
11 `Set the mouse pointer back to an arrow
12 frmMain.MousePointer = 0
13 End Sub

ENUM

ENUM

Syntax
[ Private | Public ] Enum EnumName
name1 [ = value1 ]
name2 [ = value2 ]
.
.
.
nameN [ = valueN ]
End Enum
Usage
The Enum statement defines a list of constants. Private means that the constants will be local to the module in which they are declared. Public means that you can reference the constants from anywhere in the program. If neither Public nor Private is specified, then Public will be assumed.
Arguments
EnumName--a valid Visual Basic identifier that describes the collection of constants.
name1--a valid Visual Basic identifier that is associated with a particular constant.
value1--a Long value that can be any expression including using other constants and enum values. If this value is omitted, it will default to zero.
name2--a valid Visual Basic identifier that is associated with a particular constant.
value2--a Long value that can be any expression including using other constants and enum values. If this value is omitted, it will default to one or one more than the previous constant.
nameN--a valid Visual Basic identifier that is associated with a particular constant.
valueN--a Long value that can be any expression including using other constants and enum values. If this value is omitted, it will default to one more than the previous constant.
Examples
Public Enum MyBits
bit1 = 2
bit2 = 4
Bit3 = 8
bit4 = 16
Bit5 = 32
Bit6 = 64
Bit7 = 128
End Enum
Private Sub Command1_Click()
MsgBox "The value of bit 1 in B is " & Format(Asc("B") Mod bit1)
End Sub
This program uses enumerated set of values MyBits to examine the lowest order bit of the character B. Since the ASCII value of B is 01000010 the answer will be zero.
See Also:
Const (statement), Dim (statement)


WITH STATEMENT
Syntax
With object
[ list of statements ]
End With
Usage
The With statement is used to qualify a single object or user-defined type. Properties of the qualified object may omit the object name and begin with a period (".").
Arguments
object--the name of the object to be qualified.
Examples
Private Sub Command1_Click()
With Text1
.Text = "Testing the with statement."
.Font.Size = 36
.Font.Bold = True
End With
End Sub
This routine uses a With statement to simplify assigning values to Text1 text box object.
See Also:
Object (data type)

Arrays

What Is an Array?


A collection of similar variables, in which each has the same name and all are of the same type, is an array. Remember that a variable can be thought of as a cup that holds an unknown value or an always changing value

Think of an array, then, as a collection of cups. Each cup in the collection can hold the same type of data, and every cup in the collection has the same name. Each cup within the collection is an element and has a number assigned to it that reflects its position within the collection. The first element of an array usually has a position number of 0 (zero).

Arrays can be different sizes. One array might have three elements, another might have 30 elements, and it's even possible for an array to have no elements at all--only the possibility of having elements that are created at a later time.

Declaring Arrays

You can declare, or dimension, an array in two ways: as you would a single variable and by using the To keyword.

Declaring an Array like Declaring a Single Variable
To declare an array as you would a single variable, you use the following syntax:

Dim|Public|Private ArrayName(Subscript) As DataType

In this syntax, Dim, Public, and Private are Visual Basic keywords that declare the array and its scope. If you use Dim, the array is private to the procedure in which it is declared. Public makes the array visible from anywhere in the program, and Private (within the General section of a form or module) makes the array visible only to the form or module in which it's declared. If you use Dim within a module's procedure, the array will be available to only that procedure, but if you use Dim in the module's Declarations section, the array will be available to all procedures within the module.

ArrayName is the name of the array.




Option base

When you declare an array, the first element of the array is usually 0 (zero). It's possible, however, to force the first element of an array to be 1. To do this, insert the statement Option Base 1 in the General section of a module within your project. You have to do this only when you want the first element of your array to be element number 1.

Subscript is the number of the highest element in the array. Remember that the first element in an array is usually zero, so if you declare an array in which Subscript is 6, you'll have seven element positions and, therefore, seven elements.

As is the Visual Basic keyword that signifies a type declaration.

DataType is any valid Visual Basic data type, such as Integer or Double.

Therefore, to declare an array of integers with five elements in it, you would use the following:

Dim iMyArray(4) As Integer

To assign a value to each element in the array iMyArray, you would use the following:

iMyArray(0) = 9
iMyArray(1) = 342
iMyArray(2) = 2746
iMyArray(3) = 0
iMyArray(4) = 8901

To change the value of the fourth element of the array iMyArray from 0 (zero) to 45, you would do as follows:

iMyArray(3) = 45

To declare an array of nine strings global to your entire program, you would use the code in Listing 3.10.


LISTING 3.10 LIST01.TXT--Assigning Values to an Array's Elements

01 Public strMyArray(8) As String
02
03 strMyArray(0) = "I am a pitcher."
04 strMyArray(1) = "I am a catcher."
05 strMyArray(2) = "I play first base."
06 strMyArray(3) = "I play second base."
07 strMyArray(4) = "I play third base."
08 strMyArray(5) = "I play shortstop."
09 strMyArray(6) = "I play left field."
10 strMyArray(7) = "I play center field."
11 strMyArray(8) = "I play right field."


Declaring an Array with the To Keyword


You can also declare an array by using the To keyword within the subscript syntax. For example, if you want to create an array of five Integer variables in which the first element is number 1 and the last element is number 5, you use

Dim iMyArray(1 To 5) as Integer

This method provides an easy way to start your element numbering at a value other than 0 (zero).


Dynamic arrays

Although you usually set the number of elements in an array when you declare it, it's possible to alter the size of the array. When you change the number of elements in an existing array, you redimension it.
To do so, use the ReDim keyword in the following syntax:

ReDim [Preserve] ArrayName(Subscript) As DataType

In this syntax,

ReDim is the Visual Basic keyword denoting that the array is being redimensioned.

Preserve is an optional Visual Basic keyword that forces all pre-existing elements in the array to hold their values. If you don't use the Preserve keyword when you redimension the array, the value of all elements will be changed to zero for numeric data types, and a zero-length string ("") for variable-length strings. Fixed-length strings will be filled with zeros, and variants will be initialized to EMPTY, which could be either zero or a zero-length string, depending on the expression.

ArrayName is the name of the array.

Subscript is the subscript for the highest element in the array.

As is the Visual Basic keyword that signifies a type declaration. When redimensioning an array, the As keyword is optional.

DataType is any valid Visual Basic data type, such as Integer or Double. When redimensioning an array, the DataType is optional and can't be changed with the Redim keyword unless the array is of type Variant.

Using ReDim in your code

The actual implementation of the ReDim statement is different from this conceptual illustration. If you create an array that you'll later redimension, you can't hard code the element size of the array when you first declare it. Therefore, in practice, the code here won't re dimension the array declared in Listing 3.10


The following code shows how, conceptually, to redimension the array strMyArray (from Listing 3.10) to contain 10 elements:

ReDim Preserve strMyArray(9)
strMyArray(9) = "I am the designated hitter."

To create an array that you'll later resize, you must first create the array without any elements. Listing 3.11 shows the proper way to create an array that you'll later redimension.


LISTING 3.11 LIST02.TXT--Diming an Array Without Any Elements Before
ReDiming It

01 `Create an array without any elements
02 Dim strMyArray() as String
03
04 `Dimension the array for 9 elements
05 ReDim strMyArray(8)
06
07 `Assign values to the array elements
08 strMyArray(0) = "I am a pitcher."
09 strMyArray(1) = "I am a catcher."
10 strMyArray(2) = "I play first base."
11 strMyArray(3) = "I play second base."
12 strMyArray(4) = "I play third base."
13 strMyArray(5) = "I play shortstop."
14 strMyArray(6) = "I play left field."
15 strMyArray(7) = "I play center field."
16 strMyArray(8) = "I play right field."
17
18 `Add an element and make it so all the values
19 `of the previous elements are kept intact
20 ReDim Preserve strMyArray(9)
21
22 `Assign a value to the new array element
23 strMyArray(9) = "I am the designated hitter."

Notice in Listing 3.11 that the first ReDim statement (line 5) doesn't use the Preserve keyword. You can do this because initially there are no values within the array to preserve. In the second ReDim statement on line 20, however, the Preserve keyword is very important because the pre-existing elements have assigned values that you don't want to lose. If you didn't use the Preserve keyword in the second ReDim statement, strMyArray would have the following values:

strMyArray (0) = ""
strMyArray (1) = ""
strMyArray (2) = ""
strMyArray (3) = ""
strMyArray (4) = ""
strMyArray (5) = ""
strMyArray (6) = ""
strMyArray (7) = ""
strMyArray (8) = ""
strMyArray (9) = "I am the designated hitter."

Remember, arrays are tricky. Be careful!

Multidimensional Arrays

So far, the arrays in this chapter have been one-dimensional arrays--that is, they are a one-row collection of variables, however, you can create arrays that have up
to 60 dimensions. Usually, two-dimensional arrays will suffice for most introductory programming projects, and most likely you won't need to make an array of more than three dimensions.

Think of a two-dimensional array as a tic-tac-toe board--a set of columns and rows that intersect to form a grid. Each grid cell has a location defined as ColumnNumber, RowNumber. Each element is defined by the coordinates of the column position and the row position. For example, the array element iVar(0, 0) is 5 and the element iVar(2,2) is 49.
To create a two-dimensional array, use the following syntax:

Dim|Public|Private ArrayName(SubscriptOfCols, _
SubscriptOfRows) As DataType

In this syntax,

Dim, Public, and Private are Visual Basic keywords that declare the array and its scope. If you use Dim, the array is private to the procedure in which it's declared. Public makes the array visible from
anywhere in the program, and Private (within a form or module's General section) makes the array visible only to the form or module in which it's declared. Using Dim within a module automatically
makes the array available anywhere in the program, as if the Public keyword were used.

ArrayName is the name of the array.

SubscriptOfCols is the number of the highest column in the array.

SubscriptOfRows is the number of the highest row in the array.

As is the Visual Basic keyword that denotes type declaration.

DataType is any valid Visual Basic data type.

Therefore, to declare the array you would use the following syntax:

Dim iVar(2,4) as Integer

Whereas you can consider a two-dimensional array to be a rectangle, you can consider a three-dimensional array to be a rectangular block. To declare a three-dimensional array of integers, use

Dim iVar(1,2,1) as Integer


In the array iVar, the value assigned to each element, as depicted in Figure 11.4, is as follows:

iVar(0,0,0) = 5
iVar(0,1,0) = 187
iVar(0,2,0) = 16
iVar(1,0,0) = 12
iVar(1,1,0) = 55
iVar(1,2,0) = 7
iVar(0,0,1) = 34
iVar(0,1,1) = 13
iVar(0,2,1) = 4500
iVar(1,0,1) = 612
iVar(1,1,1) = 9
iVar(1,2,1) = 784

Just as with a one-dimensional array, you can use the To keyword when declaring the subscript range of each dimension in a multidimensional array. Thus, the line

Dim dMyArray(1 To 5, 3 To 8, 3 To 5) As Double

would declare a three-dimensional array of Double values, five columns wide by six rows tall by three planes deep.

You can also use the Redim keyword to resize a multidimensional array. If you use the Preserve keyword, however, only the last dimension of a multidimensional array can be resized, and the number of dimensions can't be changed.

Using Loops to Traverse an Array

You can use the For...Next loop that you learned about in Chapter 10, "Working with Loops," to move through, or traverse, an array. This can be useful when you want to change or report values in an array.

Listing 3.12 is an example of using a For...Next loop to traverse an array. The listing is the event procedure for a button click, in which an array of 20 elements is created. A For...Next loop is used twice--first to assign values to every element in the array, and then to find the value assigned to every element and make a string that reports the value of that element

LISTING 3.12 LIST03.TXT--Using For...Next Loops to Traverse an Array

01 Private Sub cmdTraverse_Click()
02 Dim i%
03 Dim iMyArray%(19) As Integer
04 Dim BeginMsg$
05 Dim MidMsg$
06 Dim LoopMsg$
07 Dim FullMsg$
08
09 `Assign a value to each element in the array
10 `by using a loop to traverse to each element
11 `in the array.
12 For i% = 0 To 19
13 `Make the value of the element to be
14 `twice the value of i%
15 iMyArray%(i%) = i% * 2
16 Next i%
17
18 `Create the BeginMsg$ string
19 BeginMsg$ = "The element is: "
20 MidMsg$ = ", The value is: "
21 `Go through the array again and make
22 `a string to display
23 For i% = 0 To 19
24 LoopMsg$ = LoopMsg$ & BeginMsg$ & CStr(i%)
25 LoopMsg$ = LoopMsg$ & MidMsg$ & iMyArray(i%)
26
27 `Concatenate the loop message to the
28 `full message. Also add a line break
29 FullMsg$ = FullMsg$ & LoopMsg$ & vbCrLf
30
31 `Clean out the loop message so that
32 `new value next time through the loop
33 LoopMsg$ = ""
34 Next i%
35
36 txtTraverse.Text = FullMsg$
37 End Sub

The code in Listing 3.12 is simple. Remember that a For...Next loop increments a counting variable as it loops. You assign this counting variable to the element position of the array. Then, set the lower bound of the array (the lowest element number) to the start point of the loop and set the upper bound of the array (the highest element number) to the end point of the loop. When you run the loop, the counting variable will always be at an element of the array.

List Box

LISTBOXES

Of all the standard controls, the ListBox and ComboBox are best suited for categorizing and listing information from which users can make choices and selections. These controls are closely related, and the manner in which you manipulate them is almost identical. The difference is that the ComboBox combines a ListBox control with a TextBox (hence the name) and allows users to select from a list or type a new value.

Both the ListBox and ComboBox controls can handle lists. In Visual Basic, a list is an array of strings formally referred to with the List property. (The List property is common to the ListBox and ComboBox controls.) Most work that you do with ListBoxes and ComboBoxes involves adding and removing strings from the List property. You can add strings to the List property of a ListBox or ComboBox at design time or runtime.

Add strings to a ListBox or ComboBox at design time
1. Add a ListBox or a ComboBox control to the form.

2. Select this control on the form.

3. In the Properties window, select the List property and type the strings for the List property in the drop-down box. To add multiple lines to the List property, press Ctrl+Enter to
add a new line to it

At design time, the strings you added to the List property will appear in the ListBox on the form .


This design-time method for populating the List property is useful when your program starts up. Because the contents of a ListBox or ComboBox can change frequently while your program is running, however, you need to be able to add and remove items to and from the List property while it's running. To do this, the ListBox and CombBox controls make extensive use of the AddItem, RemoveItem, and Clear methods.

To add a string to the List of a Listbox or ComboBox during runtime, use the AddItem method, which has the following syntax:

Object.AddItem StringToAdd

In this syntax,

Object is the Name property of the ListBox or ComboBox.

AddItem is the Visual Basic keyword for the method.

StringToAdd is the string that you want to add to the control's list.

Listing 3.13 shows you how to use the AddItem method in a form's Load event procedure to add strings to the List of a ListBox.

LISTING 3.13 11LIST04.TXT--Using the AddItem Method to Add Items to a ListBox Control

01 Private Sub Form_Load()
02 lstHero.AddItem "Superman"
03 lstHero.AddItem "Batman"
04 lstHero.AddItem "Green Lantern"
05 lstHero.AddItem "Aquaman"
06 lstHero.AddItem "SpiderMan"
07 lstHero.AddItem "Daredevil"
08 lstHero.AddItem "Hulk"
09 End Sub

Selecting Items from a List

To understand how Visual Basic determines the value of a string selected in the List of a ListBox or ComboBox, you need to understand that a List is an array of strings. As you learned earlier in this chapter, an array is declared as follows:

Dim ArrayName(Subscript) As DataType

Therefore, if you declared a four-element array as MyArray(3), you would list the elements in that array as follows:

MyArray(0)
MyArray(1)
MyArray(2)
MyArray(3)

If you want to determine the value assigned to the second element, you could use the following statement (remember, by default the first element is MyArray(0)):

MyValue = MyArray(1)

The ListBox and ComboBox controls use a similar format. The property List is the general term that Visual Basic uses to refer to the entire array of strings within a ListBox or ComboBox. Therefore, to find
the value of the second string in the ListBox called lstHero (from Listing 11.4), you would use the following statement:

SecondString$ = lstHero.List(1)

The value of SecondString$ is "Batman".

For a ListBox or ComboBox, the selected item in a list is contained in the ListIndex property. When you select a string in a ListBox, Visual Basic assigns the position number of that string to the ListIndex
property of the control. Therefore, to determine the value of the selected string in lstHero (from Listing 3.4), you would use the following code:

Private Sub lstHero_Click()
lblHero.Caption = lstHero.List(lstHero.ListIndex)
End Sub

This is the event procedure for the Click event of the lstHero ListBox control. When the user clicks a string in the ListBox, the code executes and reports back the value of the selection within the Caption property of the Label named lblHero. Figure 11.10 shows what happens when the user selects a string.


A faster way to find out the value of the string a user selects in a ListBox or ComboBox is to use the Text property. For example, you can use the following for a ListBox:

Dim strMyStr as String
strMyStr = List1.Text

For a ComboBox, use the following:

Dim strMyStr as String
strMyStr = Combo1.Text

Removing Items from a List


You remove a string from a list in a ListBox or ComboBox by using the RemoveItem method:

Object.RemoveItem Index

In this syntax

Object is the Name property of the ListBox or ComboBox.

RemoveItem is the Visual Basic keyword for the method used to remove items from a list.

Index is the position in the List property of the string that you want to remove from the control. To remove a selected item from a list, use the ListIndex property of the control.

Listing 3.14 shows the RemoveItem method used in code.


LISTING 3.14 LIST05.TXT--Using the RemoveItem Method to Remove a String
from a ListBox

01 Private Sub cmdRemove_Click()
02 lstHero.RemoveItem (lstHero.ListIndex)
03 lblHero.Caption = ""
04 End Sub

When you remove an item from a ListBox or ComboBox, make sure that you clear the text from the Caption property of the Label control lblHero, as shown in line 3 of Listing 3.14. If you don't do this, the user will remove the string from the ListBox, but it will remain in the Label control.


Clearing a List


If you want to remove all the strings contained in a ListBox or ComboBox, use the Clear method:

Object.Clear

In this syntax

Object is the Name property of the ListBox or ComboBox.

Clear is the Visual Basic keyword for the method that removes all items from a list.

lstHero.Clear

ComboBox Styles

Understanding ComboBox Styles

The ListBox and ComboBox controls have much in common, but each has a distinct use. A ListBox takes up more room than a ComboBox, and when using a ListBox, you can't select or input unlisted data. A ComboBox offers more flexibility and uses a form's space more efficiently.

A ComboBox's Style property enables you to change the operational characteristics and appearance of the control.

Using a simple combo ComboBox

When you first add a ComboBox with the 1 - Simple Combo style to a form, the ComboBox is sized so that none of the ListBox is displayed. Increase the Height property to display more of the ListBox.


Values for the ComboBox Style Property

0 - Drop-down Combo
A drop-down list. However, users can also enter new data to the ComboBox by inputting text directly into the TextBox portion of the ComboBox.
1 - Simple Combo
A combination of a TextBox and a ListBox that doesn't drop down. Users can select data from the ListBox or type new data into the TextBox. The size of a simple ComboBox
includes both edit and list portions.
2 - Drop-down List
A read-only drop-down list from which users can only select data. Users can't enter data into the control.

The most common ComboBox styles are 0 - Drop-down Combo and 2 - Drop-down List. As mentioned earlier, you use a drop-down ComboBox style when you want to let users add new data that's not in the ComboBox's list. You use the drop-down list style when you want users to choose from data that's only in the drop-down list of the ComboBox.

Using Arrays, ComboBoxes, and ListBoxes in a Sample Program


The Baseball ScoreKeeper program uses many things you've just learned. It uses arrays to keep track of each inning's score throughout the game and uses For...Next loops to traverse arrays to set and get particular values of an array's elements. It also uses arrays with a ListBox to present a team's roster and to display which player is now at bat.

To use the program, users pick the team now at bat by clicking the appropriate OptionButton at the left of the form. Then users can select the current player from a ListBox that lists the players on the team. At the end of an inning, users enter the inning number and the number of runs scored into two TextBoxes at the bottom of the form. Then users click the Add Runs button to display the runs scored during
the inning and the total score in the appropriate scoreboard and total score Label controls.


Examining ScoreKeeper's Event Procedures


The bulk of the work in the Baseball ScoreKeeper program takes place within the code of two event procedures, Form_Load() and cmdAddRun_Click().

How the Form_Load() procedure initializes the program

1. It declares local variables for scorekeeping and the scoreboard string and declares one array for each team's roster. Finally, it redimensions the two global scorekeeping arrays (these are declared in the module modBaseBall, one for each team) to nine elements. The elements of these arrays will hold the score for each inning.

2. It uses a For...Next loop to traverse each element in the Team One scorekeeping array, gTeamOneInnings(). Within each iteration of the loop, it takes the value in each element and adds it to the variable TotalScore%, which holds the total score for all the innings. Then Form_Load converts the value in each element to a character by using the CStr() function and combines this converted
character with the | character to form a larger scorekeeping string variable, InningString$ Next, the value of the InningString$ variable is assigned to the Caption property of the Scoreboard label, and the value of the TotalScore% variable is assigned to the Caption property of the total runs Label. Finally, the InningString$ variable is reset, and this step is repeated for Team Two.

3. It redimensions the team roster arrays for both teams to have nine elements. Then it adds the names and playing positions of each player to the element that corresponds to the player's position in the batting order.

4. It uses a For...Next loop with the AddItem method of each team's ListBox to traverse the roster arrays of each team and add the value of each element in the array (player name and playing position) to the respective ListBox.


Listing 3.15 shows these steps within the code of the Form_Load() event procedure.


LISTING 3.15 LIST.06.TXT--Initializing the Main Form of the Baseball
Scorekeeper Application with the Form_Load() Event Procedure

01 Private Sub Form_Load()
02 `=====================STEP ONE====================
03 Dim i% `Counter variable
04 Dim TotalScore%
05 Dim InningsString$ `String to display score for inning
06 Dim TeamOneRoster$() `Array to hold players' names
07 Dim TeamTwoRoster$() `Array to hold players' names
08 `Redimension the global score arrays
09 `DEFAULT_INNINGS is a constant declared in the module,
10 `modBaseBall
11 ReDim gTeamOneInnings(DEFAULT_INNINGS - 1)
12 ReDim gTeamTwoInnings(DEFAULT_INNINGS - 1)
13
14 `=====================STEP TWO====================
15 `Initialize the default score for Team One
16
17 TotalScore% = 0
18 For i% = 0 To DEFAULT_INNINGS - 1
19
20 InningsString$ = InningsString$ _
& CStr(gTeamOneInnings(i%)) & " | "
21 Next i%
22
23 `Display the concatenated string in the score label
24 lblTeamOne.Caption = InningsString$
25 `Display the total score for Team One
26 lblTeamOneScore.Caption = CStr(TotalScore%)
27
28 `Clean out score string for new trip through
29 `Team Two's score
30 InningsString$ = ""
31
32 `Initialize the default score for Team Two
33 For i% = 0 To DEFAULT_INNINGS - 1
34 InningsString$ = InningsString$ _
& CStr(gTeamTwoInnings(i%)) & " | "
35 Next i%
36 `Display the concatenated string in the score label
37 lblTeamTwo.Caption = InningsString$
38 `Display the total score for Team Two
39 lblTeamTwoScore.Caption = CStr(TotalScore%)
40 `=====================STEP THREE====================
41 `Make room in the roster arrays for 9 players
42 ReDim TeamOneRoster$(8) `Array elements begin at zero
43 ReDim TeamTwoRoster$(8)
44
45 `Add the players for Team One to the roster array
46 TeamOneRoster$(0) = "Phillips, lf"
47 TeamOneRoster$(1) = "Palmero, cf"
48 TeamOneRoster$(2) = "Erstad, 1b"
49 TeamOneRoster$(3) = "Hollins, 3b"
50 TeamOneRoster$(4) = "Salmon, rf"
51 TeamOneRoster$(5) = "Leyritz, dh"
52 TeamOneRoster$(6) = "Garne, c"
53 TeamOneRoster$(7) = "Gerbeck, 2b"
54 TeamOneRoster$(8) = "DiScensa, ss"
55
56 `Add the players for Team Two to the roster array
57 TeamTwoRoster$(0) = "Garciaparra, ss"
58 TeamTwoRoster$(1) = "Valentibn, 3b"
59 TeamTwoRoster$(2) = "Vaughn, 1b"
60 TeamTwoRoster$(3) = "Jefferson, dh"
61 TeamTwoRoster$(4) = "Cordero, lf"
62 TeamTwoRoster$(5) = "O'Leary, rf"
63 TeamTwoRoster$(6) = "Mack, cf"
64 TeamTwoRoster$(7) = "Hittenberg, c"
65 TeamTwoRoster$(8) = "Frey, 2b"
66
67 `=====================STEP FOUR====================
68 `Traverse the roster arrays and add the contents
69 `the the roster listboxes.
70 For i% = 0 To 8
71 lstTeamOne.AddItem (TeamOneRoster(i%))
72 lstTeamTwo.AddItem (TeamTwoRoster(i%))
73 Next i%
74
75 End Sub

The cmdAddRun_Click() event procedure is the code that adds the runs scored in a given inning to the Scoreboard when the user clicks the Add Runs button.


How the cmdAddRun_Click() procedure works
1. It creates variables that reflect the current inning and current score. It also creates a counter variable and variables that hold the total score and the scoreboard string.