This lesson taught you how to work with advanced array data as well as how to place option button and check box controls on
the form. The arrays simplify your programs by letting you step through data and controls using For loops. The option buttons and
check boxes offer your users additional ways to select from choices that your program offers.
You can now add
control to your programs by putting loops in the code that repeat sections of the program. Computers are excellent tools for repeating calculations as many times as necessary to produce results. Loops also provide ways for you to check
user input against
good and bad values and repeat the input prompting as many times as necessary for the user to enter data in the required format and range.
In this lesson, you saw the following:
Figure P6.1 shows the PROJECT6.MAK application as soon as you load and run the program. The project is simple but uses two sets of framed option buttons and two control arrays of option buttons. The project's controls are as follows:
Figure P6.1. The project's opening screen.
The application is
simple because of the control arrays. Control arrays greatly simplify an application in which several similar controls are needed to perform similar functions. Without the control arrays, this extremely introductory project
would be burdened by at
least twenty event procedures just to perform the same task that it currently performs with only three event procedures!
When you first run the program, the form's background color is set to Black simply because the first option button in the left frame is selected. None of the beeping options is set when the program first executes.
You can select one of the beeping options in addition to the color options. Remember that when option buttons appear in separate frames, the option buttons are grouped to allow for one selection from each group. Without the frames, you could
select a
color or select a beep option button, but not both.
Click the second beeping option button and you'll hear two beeps. Select a different color option button and the form's background color changes accordingly.
Listing P6.1 contains PROJECT6.MAK's event procedures. The Index value sent to the optBeep_Click() and to the
optColor_Click() event procedures are used inside each procedure to beep or set an appropriate form color.
Note: The QBColor() function is a built-in Visual Basic that returns a hexadecimal color code so that you don't have to know anything about hexadecimal values. When you use an integer value from 0 to 15 inside QBColor()'s parentheses, Visual Basic function sets a color that matches that number. The colors correspond to the order shown on the form. The Index values for the sixteen color values are numbered from 0 to 15, so the event procedure can use that color code inside QBColor().
Listing P6.1. The color changing, beeping, and exit event procedures.
1: Sub optCol_Click (Index As Integer) 2: ' One of the color option buttons 3: ' triggered this event 4: frmOpt.BackColor = QBColor(Index) 5: End Sub 6: Sub optBeep_Click (Index As Integer) 7: ' The beeping option buttons have Index 8: ' properties that begin with 1 that 9: ' correspond to the number of beeps. 10: Dim Ctr As Integer 11: Dim Delay As Long 12: For Ctr = 1 To Index 13: Beep 14: For Delay = 1 To 80000 15: ' Do nothing but waste time... 16: Next Delay 17: Next Ctr 18: End Sub 19: Sub cmdExit_Click () 20: End 21: End Sub
1: The color-changing option buttons make up the control array named optCol, so the name of the Click event procedure is optCol_Click(). No matter which of the
16 color option buttons the user clicks, this event procedure executes.
2: A remark helps explain the purpose of the event procedure.
3: A remark helps explain the purpose of the event procedure.
4: Sets the form's background
color property to the QBColor() value of the Index that corresponds to the option button clicked.
5: Terminate the event procedure.
5: Without control arrays, each option button would require its own event procedure.
6: The beeping option buttons make up the control array named optBeep, so the name of the Click event procedure is optBeep_Click(). No matter
which of the 3 beeping option buttons the user clicks, this event procedure executes.
7: A remark helps explain the purpose of the event procedure.
8: A remark helps explain the purpose of the event procedure.
9: A
remark helps explain the purpose of the event procedure.
10: Define an integer variable used for the loop control variable.
11: Define a long integer variable used for slowing down the loop.
12: Loops enough times to match the
option button's Index value, which, according to the Property window, contains the values from 1 to 3.
13: Beep the computer's speaker.
14: Begin a huge For loop that slows down the beeps.
15: A remark is the only thing inside
the innermost nested loop.
15: A nested loop slows down the computer's beeping speed.
16: Continue wasting some time by
repeating the inner loop.
17: Beep again if the beeps have not completed yet.
18: Terminate the event procedure.
You can now exit the application and exit Visual Basic.
Are you getting tired of beeping and color-changing programs? The simple applications that you've been working with have kept your mind focused on the Visual Basic environment and language. You've now reached the halfway point of this book's education. You have enough tools in your Visual Basic programming repertoire to begin writing more powerful applications, and you'll do just that in the next lesson.