vel4p07






Visual Basic 4 in 12 Easy Lessons vel4p07.htm













Visual Basic 4 in 12 Easy Lessons vel4p07.htm
























The Program's Description



The Program's Action



The Time Zone Change Routine



Description



Computing Retirement



Description



Close the Application









Project 7









Functions and Dates



Stop and Type: This lesson taught you all about Visual Basic's built-in numeric and string functions. These functions save you loads of programming time because you won't have to take the time or make the effort to write code that performs common tasks. There are numeric functions for number conversions and for scientific and mathematical applications. The string functions manipulate and change strings to match the format you need.



In addition to the numeric and string functions, Visual Basic also includes a comprehensive set of date, time, and formatting functions that perform tasks unheard of in many other programming languages.



In this lesson, you saw the following:





Why functions speed program development





When to use one of the three integer conversion functions





What math and scientific functions Visual Basic provides





Which date and time functions to use when you need to extract or compute date and time values





How the Format() function enables you to determine how output will look















The Program's Description



Figure P7.1 shows the PROJECT7.VBP application as soon as you load and run the program. The project's form contains a frame with four option buttons and two command buttons.



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











Note: The time zone calculations used in this project assume that you reside in the Central Standard Time zone. Of course, you may or may not live in the CST zone, but the program has to assume some kind of zone to base the time zone change calculations.







The program contains a short Form_Load() subroutine that contains only the single line that follows:







lblTime.Caption = Format$(Time, "Long Time")

The Format$() function formats the initial time value to display the time in a Long Time format. As Unit 14 explained, the Long Time format displays time values using hours, minutes, and seconds.



The program's option buttons select a different city for a local time display in the large time label inside the frame. The time updates only when you first run the program and when you click an option button.



The Birthday Fun command button calculates the year in which you can retire and go on that two-year cruise that you've always dreamed of.











The Program's Action



Try this: Click through the various option button city selections to see the time zone changes, both in three U.S. time zones and also the Italian time zone change in Rome. The option buttons form a series of elements in an option button control array, and the index values for the option buttons help the code beneath the buttons (shown in the next section) determine which time zone to compute.











Before looking at the code for this project, try to determine how you would add or subtract to and from the current time zone, using Visual Basic code, to compute a different time zone. The practice will do you a lot of good and will help solidify the previous unit's time function discussion.







Click the Birthday Fun command button and you'll see the input box shown in Figure P7.2 that asks the user for his or her birthday. A subsequent message box then tells the user the year that the user can retire, based on a retirement age of 65.



Figure P7.2. An input box gets the user's birthday.











The Time Zone Change Routine



Listing P7.1 contains the event procedure for the city time zone option buttons stored in the control array named optCity.





Input: Listing P7.1. Determining the time in different time zones.





1: Sub optCity_Click (Index As Integer)

2: ' Adds or subtracts a value from the time

3: ' that matches the time zone selected

4: Dim DiffVal As Integer ' Hours to add or subtract

5:

6: ' Test the option button's index

7: Select Case Index

8: Case 0:

9: ' NYC

10: DiffVal = 1

11: Case 1:

12: ' Tulsa, the Central Standard Time default

13: DiffVal = 0

14: Case 2:

15: ' Los Angeles

16: DiffVal = -2

17: Case 3:

18: ' Rome

19: DiffVal = -7

20: End Select

21: lblTime.Caption = Format$(TimeSerial(Hour(Now) + DiffVal, Minute(Now),

_Second(Now)), "Long Time")

22: End Sub









Description



1. The option button array is named optCity, so the name of the Click event procedure is optCity_Click().



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



3. The remark continues that helps explain the purpose of the event procedure.



4. Define an array that will hold the number of hours to add or subtract to the current time to obtain a new time zone value.



5. A blank line to separate the variable definition from the rest of the code.



6. A remark helps explain the purpose of the subsequent Select Case.



7. Begin selecting the proper code to execute based on the index of the option button selected.



8. A match for the first option button (labeled New York).



9. A comment describing this case selection.



10. Add one hour to the current (Central Standard Time assumed) time value.



11. A match for the second option button (labeled Tulsa).



12. A comment describing this case selection.



13. No change to the current Central Standard Time value.



14. A match for the third option button (labeled Los Angeles).



15. A comment describing this case selection.



16. Subtract two hours from the current (Central Standard Time assumed) time value.



17. A match for the fourth option button (labeled Rome).



18. A comment describing this case selection.



19. Subtract seven hours from the current (Central Standard Time assumed) time value.



20. Terminate the body of the Select Case.



21. Update the caption with the new formatted time value. The TimeSerial() function allows you to specifically change only the hour.



22. Terminate the event procedure.











21. with Time-Serial(), you have access to separate parts of the time.















Computing Retirement



When the user clicks the Birthday Fun command button, the program asks for the user's birthdate and calculates the retirement year based on the user's birthday. Listing P7.2 contains the event procedure for the retirement age calculation.





Input: Listing P7.2. Calculating the year of retirement.





1: Sub cmdBirth_Click ()

2: ' Asks the user for a birthdate and determine retirement

3: Dim BDate As Variant

4: Dim BYear, RetYear As Integer

5: ' Get the birthday

6: Do

7: BDate = InputBox("When is your birthday (dd/mm/yy)?", "Birthday")

8: Loop Until IsDate(BDate) Or BDate = ""

9: If BDate <> "" Then ' If not Cancel

10: ' Filter out the birth year

11: BYear = Year(BDate)

12: ' Add the number of years to retirement

13: RetYear = BYear + 65

14: MsgBox "You can retire in" & Str$(RetYear)

15: End If

16: End Sub









Description



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



2. A remark explains the purpose of the procedure.



3. Define a variant variable that will hold the user's birthdate.



4. Define two integer variables. One will hold the user's birth year and the other will hold the year of retirement.



5. A remark that explains the purpose of the subsequent code.



6. Begin a loop to get a valid date.



7. Ask the user for his or her birthdate.



8. Keep looping until the user enters a valid date or until the user presses Cancel.



9. Perform retirement routine on date as long as the user doesn't press Cancel.



10. A remark that explains the purpose of the subsequent code.



11. Strip off the year of the birthdate and save the year.











11. Year() pulls the year from a date value.







12. A remark that explains the purpose of the subsequent code.



13. Add 65 to the birth year to find the year of retirement.



14. Display the retirement message.



15. Terminate the If statement.



16. Terminate the event procedure.











Close the Application



You can now exit the application and exit Visual Basic. The next lesson explains how to build more complex programs by splitting common code that you write into several separate non-event procedures.


















Wyszukiwarka

Podobne podstrony:
vel4p09
vel4p08
vel4p01
vel4p02
vel4p06
vel4p04
vel4p03
vel4p05

więcej podobnych podstron