vel4p05






Visual Basic 4 in 12 Easy Lessons vel4p05.htm













Visual Basic 4 in 12 Easy Lessons vel4p05.htm
























The Program's Description



The Program's Action



The Color Changing Routine



Description



Getting Odd Values



Close the Application









Project 5









Gaining Control



Stop and Type: This lesson taught you how to document and control programs. As you increase the power of your Visual Basic applications, they become more complex. As programs get more complex, program maintenance becomes more difficult. Remarks help document summary information about the program and provide more specific help for tricky lines of code.



You can now add control to your programs by putting loops in the code, which 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 to repeat the input prompting so that the user enters data in the required format and range.



In this lesson, you saw the following:





How remarks help document your code





When message boxes offer the user more help than label controls





How to get feedback from input boxes





What kind of power the loops add to your programs















The Program's Description



Figure P5.1 shows how the PROJECT5.VBP application looks as soon as you load and run the program. The project's form contains four command buttons. The command buttons perform different tasks that demonstrate the various topics of this lesson.



Figure P5.1. The project's opening screen.



The program's three center command buttons perform the following actions:





The Color Change command button quickly steps the form's background color through a series of color changes.





The Odd Entry command button keeps requesting an odd number until the user enters an odd number.





The Even Entry command button keeps requesting an even number until the user enters an even number.















The Program's Action



Click the Color Change command button. Watch closely. The form's background changes colors extremely quickly while beeping. Although the colors change so rapidly that you cannot see all the colors, the program changes the form's background color, by means of the form's BackColor property value, sixteen times.



The beeping is accomplished through four Beep statements that do nothing more than ring the PC's bell four times between each color change. The true purpose of the beeping is to slow down the color changes. On most computers, the colors would change so fast without the intermediate beeps, that you would not see anything except a blur. Even with the beeping, the colors change almost faster than the eye can see.



The Odd Entry command button asks the user for an odd number, using the input box shown in Figure P5.2. The command button's Click event procedure keeps looping until the user enters an odd number.



Figure P5.2. The program requests an odd number.



The third command button, Even Entry, requests an even number and keeps looping until the user enters an even number.











The Color Changing Routine



Listing P5.1 contains the event procedure for the Color Change command button's Click procedure.





Listing P5.1. The color-changing event procedure.





1: Sub cmdColor_Click ()

2: ' Wildly Change the color of the form

3: Dim ColorVal As Integer

4: For ColorVal = 0 To 15 ' Step through the color values

5: frmControl.BackColor = QBColor(ColorVal)

6: Beep ' These beeps simply add

7: Beep ' to the intensity of the

8: Beep ' color change and slow

9: Beep ' things down some

10: Next ColorVal

11: End Sub









Description



1. The command button is named cmdColor, so the name of the Click event procedure is cmdColor_Click().



2. A remark helps explain the purpose of the event procedure.



3. An integer variable holds a For loop's color-changing value.



4. This line begins a For loop that iterates 16 times, assigning the numbers 0 through 15 to the variable named ColorVar.



5. With the built-in QBColor() function you easily can change color values.











5. The built-in function named QBColor() requires a number in its parentheses from 0 to 15. Each number represents a different color value. Using QBColor() is easier than specifying a hexadecimal value.







6. A Beep statement slows down the color change and adds a little spice to the program.



7. A Beep statement slows down the color change and adds a little spice to the program.



8. A Beep statement slows down the color change and adds a little spice to the program.



9. A Beep statement slows down the color change and adds a little spice to the program.



10. This line terminates the For loop.



11. This line terminates the event procedure.











Getting Odd Values



When the user clicks the Odd Entry button, the program asks for an odd number, using the InputBox$() function taught in this lesson. The InputBox$() function captures a string value. If the user clicks Cancel, the form window returns. Otherwise, the event procedure expects an odd number and keeps requesting that number using a Do loop until the user types an odd number.





Listing P5.2. The event procedure that requests an odd number. Description





1: Sub cmdOdd_Click ()

2: ' Request an odd number

3: Dim OddStr As String

4: Dim OddNum As Integer

5: Do

6: OddStr = InputBox$("Enter an odd number", "Get Odd")

7: If (OddStr = "") Then ' Quit if pressed Cancel

8: Exit Do ' Quits the Do loop early

9: End If

10: OddNum = Val(OddStr) / 2

11: ' The integer OddNum holds the exact value

12: ' of the Val(OddStr) / 2 if OddNum is even

13: Loop Until (OddNum <> (Val(OddStr) / 2))

14: End Sub

Description



1. The command button's Name property contains cmdOdd, so the name of the Click event is cmdOdd_Click().



2. A remark explains the purpose of the procedure.



3. This line defines a string variable that will hold the result of the InputBox$() function.



4. This line defines an integer variable that will hold the result of the InputBox$() function.



5. This line begins a loop.



6. Here the program collects the user's response to the request for an odd number, and it displays an appropriate input box title.



7. If the user presses the Cancel command button, the program exits the loop.



8. This line terminates the Do loop.



9. This line is the end of the body of the If statement.



10. This line converts the input string to a number, divides the number by two, and stores the result.



11. A multiline remark describes the check for an odd number.



12. This line continues the remark.



13. OddNum holds an integer value calculated and stored in line 10.



14. This line terminates the event procedure.











The cmdEven_Click() event procedure is identical to cmdOdd_Click() except that it checks for an even number instead of an odd number.















Close the Application



You can now exit the application and exit Visual Basic. The next lesson explains more Visual Basic commands and describes how to add new controls to your form that process and display large amounts of data.


















Wyszukiwarka

Podobne podstrony:
vel4p09
vel4p08
vel4p01
vel4p02
vel4p06
vel4p04
vel4p03
vel4p07

więcej podobnych podstron