vel4p06






Visual Basic 4 in 12 Easy Lessons vel4p06.htm













Visual Basic 4 in 12 Easy Lessons vel4p06.htm
























The Program's Description



The Program's Action



The Event Procedures



Close the Application









Project 6









Combining Code and Controls



Stop and Type: This lesson taught you how to work with advanced array data as well as how to place option button and checkbox 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:





How arrays help simplify the definition of multiple variables





How control arrays help work with multiple controls (instead of several event procedures, you need to write only one)





When to use check boxes and when to use option buttons





Where to use framed option button groups to create sets of option buttons from which the user can select















The Program's Description



Figure P6.1 shows the PROJECT6.VBP 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.





Two frames, one holding 15 color option buttons and the second holding three beeping options





Fifteen option buttons inside the left frame that select a background color for the form





Three option buttons inside the right frame that determine how many beeps the user wants to hear





An Exit command button to terminate the application







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!











The Program's Action



When you first run the program, the form's background color is set to Blue. 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.











The Event Procedures



Listing P6.1 contains PROJECT6.VBP'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 function 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, the 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 16 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, use the button's Index value

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









Close the Application



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.


















Wyszukiwarka