Saturday, December 26, 2009

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.

No comments:

Post a Comment