IsNumeric() is a Visual Basic function that checks a string number to see whether it looks like a number. If the string looks like a number, IsNumeric() returns True; otherwise, it returns False.
IsNumeric() is often used to validate user input.
2. It takes the text entered in the Inning TextBox, inspects it to make sure that it's a number by using the IsNumeric() function, and makes sure that the user didn't enter a number higher than the
number of innings in the game so far. If this value is acceptable, the procedure assigns it to the current inning variable, CurrentInning%. cmdAddRun_Click() does the same sort of IsNumeric() check on the text in the Runs Scored TextBox, txtRuns. If this is acceptable, the procedure assigns it to the current score variable, CurrentScore%.
3. The procedure checks the value of the OptionButton for Team One. If the OptionButton is selected, its value will be True and the value in the current score variable, CurrentScore%, will be assigned to an element in gTeamOneInnings(), the global array that holds the values for Team One's per-inning score. The current score is assigned to the element position that's one less than the value of the current inning variable, CurrentInning%. This is because the first element in the gTeamOneInnings array is zero. If the value of the OptionButton for Team One is False, the OptionButton for Team Two must have been selected and this step should be done in terms of Team Two.
4. It traverses the global arrays that hold the value of every inning's score for each team to determine the total runs scored for each team. Then the procedure constructs the scoreboard string and assigns the results to the appropriate controls. (This is an exact repeat of the work done in the fourth step of the Form_Load() event procedure.)
Listing 3.16 shows the event procedure for the cmdAddRuns_Click event.
LISTING 3.16 LIST07.TXT--Determining the Team and Inning to Which the
Scored Runs Apply
01 Private Sub cmdAddRun_Click()
02 `=====================STEP One====================
03 Dim CurrentInning%
04 Dim CurrentScore%
05 Dim i%
06 Dim TotalScore%
07 Dim InningsString$
08
09 `=====================STEP TWO====================
10 `Convert the text in the txtInning to an Integer if
11 `indeed the text looks like a number
12 If IsNumeric(txtInning.Text) Then
13 CurrentInning% = CInt(txtInning.Text)
14 Else
15 CurrentInning% = 1
16 End If
17
18 `Make sure the inning number is not more than 9
19 If CurrentInning% > DEFAULT_INNINGS Then
20 CurrentInning% = DEFAULT_INNINGS
21 End If
22
23 `Convert the text in the txtRuns to an Integer if
24 `indeed the text looks like a number
25 If IsNumeric(txtRuns.Text) Then
26 CurrentScore% = CInt(txtRuns.Text)
27 Else
28 CurrentScore% = 0
29 End If
30 `=====================STEP THREE===================
31 `Set the score to the designated inning for the team
32 `identified by the check option box.
33 If opTeamOne.Value = True Then
34 gTeamOneInnings(CurrentInning% - 1) = CurrentScore%
35 Else
36 `If TeamOne.Value is not true, then TeamTwo.Value must
37 `be True. It's a logic thing!
38 gTeamTwoInnings(CurrentInning% - 1) = CurrentScore%
39 End If
40
41 `Set the new score for Team One
42 For i% = 0 To DEFAULT_INNINGS - 1
43 TotalScore% = TotalScore% + gTeamOneInnings(i%)
44 InningsString$ = InningsString$ _
& CStr(gTeamOneInnings(i%)) & " | "
45 Next i%
46 `=====================STEP FOUR===================
47 `Display the concatenated string in the score label
48 lblTeamOne.Caption = InningsString$
49 `Display the total score for Team One
50 lblTeamOneScore.Caption = CStr(TotalScore%)
51
52 `Clean out score string for new trip through
53 `Team Two's score
54 InningsString$ = ""
55 `Clean out the total score integer variable
56 TotalScore% = 0
57
58 `Set the new score for Team Two
59 For i% = 0 To DEFAULT_INNINGS - 1
60 TotalScore% = TotalScore% + gTeamTwoInnings(i%)
61 InningsString$ = InningsString$ _
& CStr(gTeamTwoInnings(i%)) & " | "
62 Next i%
63
64 `Display the concatenated string in the score label
65 lblTeamTwo.Caption = InningsString$
66 `Display the total score for Team One
67 lblTeamTwoScore.Caption = CStr(TotalScore%)
68
69 End Sub
The last thing that the program does is report who is at bat. This occurs in the Click event of the ListBox for each team's roster whenever the user selects a player. Listing 3.17 shows the code for Team One's ListBox Click event procedure. This code uses the List and ListIndex properties that you learned about in the section "Selecting Items from a List."
LISTING 3.17 11LIST08.TXT--Code for the lstTeamOne_Click Procedure
01 Private Sub lstTeamOne_Click()
02 `Have the name that the user clicks appear in the
03 `at bat label
04 lblTeamOneAtBat.Caption = lstTeamOne.List(lstTeamOne.ListIndex)
05 End Sub
The only difference between the lstTeamOne_Click and lstTeamTwo_Click event procedures is that the first references the Team One at bat Label and ListBox, and the other references the Team Two at
bat Label and ListBox.
Some potential problems still exist:
What happens if the user enters a letter character such as "a" in the Runs Scored or Inning text box? You need to provide an error check on these text boxes, possibly using the IsNumeric() function.
What if a game goes into extra innings? You need to Redim the scorekeeping arrays.
How do you remind users to change the player at bat if they forget, and what happens if users forget to change the team at bat? You might set a variable that tracks the most recently accessed player or team and provide a message box if this variable matches the new player or team when the Add Runs button is clicked.
You can easily solve these and other problems; for the most part, you possess the tools you need to address them. All that's required is a little thought and some experimentation.
Saturday, December 26, 2009
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment