📄 velp07.htm
字号:
<HTML><HEAD><TITLE>Visual Basic in 12 Easy Lessons velp07.htm </TITLE><LINK REL="ToC" HREF="index.htm"><LINK REL="Index" HREF="htindex.htm"><LINK REL="Next" HREF="vel15.htm"><LINK REL="Previous" HREF="vel14.htm"></HEAD><BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080"><A NAME="I0"></A><H2>Visual Basic in 12 Easy Lessons velp07.htm</H2><P ALIGN=LEFT><A HREF="vel14.htm" TARGET="_self"><IMG SRC="purprev.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Previous Page"></A><A HREF="index.htm" TARGET="_self"><IMG SRC="purtoc.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="TOC"></A><A HREF="vel15.htm" TARGET="_self"><IMG SRC="purnext.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Next Page"></A><HR ALIGN=CENTER><P><UL><UL><UL><LI><A HREF="#E68E111" >Stop & Type</A><UL><LI><A HREF="#E69E97" >The Program's Description</A><LI><A HREF="#E69E98" >The Program's Action</A><LI><A HREF="#E69E99" >The Time Zone Change Routine</A><LI><A HREF="#E69E100" >Descriptions</A><LI><A HREF="#E69E101" >Computing Retirement</A><LI><A HREF="#E69E102" >Descriptions</A><LI><A HREF="#E69E103" >Close the Application</A></UL></UL></UL></UL><HR ALIGN=CENTER><A NAME="E66E21"></A><H1 ALIGN=CENTER><CENTER><FONT SIZE=6 COLOR="#FF0000"><B>Project 7</B></FONT></CENTER></H1><BR><A NAME="E67E24"></A><H2 ALIGN=CENTER><CENTER><FONT SIZE=6 COLOR="#FF0000"><B>Functions and Dates</B></FONT></CENTER></H2><BR><BR><A NAME="E68E111"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>Stop & Type</B></FONT></CENTER></H3><BR><P>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 for mathematical applications. The string functions manipulate and change strings to match the format you need.<BR><P>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.<BR><P>In this lesson, you saw the following:<BR><UL><LI>Why functions speed program development<BR><BR><LI>When to use one of the three integer conversion functions<BR><BR><LI>What math and scientific functions Visual Basic provides<BR><BR><LI>Which date and time functions to use when you need to extract or compute date and time values<BR><BR><LI>How the Format() function allows you to determine how output will look<BR><BR></UL><BR><A NAME="E69E97"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>The Program's Description</B></FONT></CENTER></H4><BR><P>Figure P7.1 shows the PROJECT7.MAK 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.<BR><P><B> <A HREF="P7vel01.gif">Figure P7.1. The project's opening screen.</A></B><BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Note: </B>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.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>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.<BR><P>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.<BR><BR><A NAME="E69E98"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>The Program's Action</B></FONT></CENTER></H4><BR><P>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.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Tip: </B>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.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>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.<BR><P><B> <A HREF="P7vel02.gif">Figure P7.2. An input box gets the user's birthday.</A></B><BR><BR><A NAME="E69E99"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>The Time Zone Change Routine</B></FONT></CENTER></H4><BR><P>Listing P7.1 contains the event procedure for the city time zone option buttons stored in the control array named optCity.<BR><P><FONT COLOR="#000080"><B>Listing P7.1. Determining the time in different time zones.</B></FONT><BR><PRE><FONT COLOR="#000080">1: Sub optCity_Click (Index As Integer)2: ' Adds or subtracts a value from the time3: ' that matches the time zone selected4: Dim DiffVal As Integer ' Hours to add or subtract5:6: ' Test the option button's index7: Select Case Index8: Case 0:9: ' NYC10: DiffVal = 111: Case 1:12: ' Tulsa, the Central Standard Time default13: DiffVal = 014: Case 2:15: ' Los Angeles16: DiffVal = -217: Case 3:18: ' Rome19: DiffVal = -720: End Select21: lblTime.Caption = Format$(TimeSerial(Hour(Now) + DiffVal, Minute(Now), Second(Now)), "Medium Time")22: End Sub</FONT></PRE><BR><A NAME="E69E100"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>Descriptions</B></FONT></CENTER></H4><BR><P>1: The option button array is named optCity, so the name of the Click event procedure is optCity_Click(). The index value also is passed to the event procedure so that the procedure knows which option button triggered the event.<BR><P>2: A remark helps explain the purpose of the event procedure.<BR><P>3: The remark continues that helps explain the purpose of the event procedure.<BR><P>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.<BR><P>5: A blank line to separate the variable definition from the rest of the code.<BR><P>6: A remark helps explain the purpose of the subsequent Select Case.<BR><P>7: Begin selecting the proper code to execute based on the index of the option button selected.<BR><P>8: A match for the first option button (labeled New York).<BR><P>9: A comment describing this case selection.<BR><P>10: Add one hour to the current (Central Standard Time assumed) time value.<BR><P>11: A match for the second option button (labeled Tulsa).<BR><P>12: A comment describing this case selection.<BR><P>13: No change to the current Central Standard Time value.<BR><P>14: A match for the third option button (labeled Los Angeles).<BR><P>15: A comment describing this case selection.<BR><P>16: Subtract two hours from the current (Central Standard Time assumed) time value.<BR><P>17: A match for the fourth option button (labeled Rome).<BR><P>18: A comment describing this case selection.<BR><P>19: Subtract seven hours from the current (Central Standard Time assumed) time value.<BR><P>20: Terminate the body of the Select Case.<BR><P>21: Update the caption with the new formatted time value. The TimeSerial() function allows you to specifically change only the hour.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE>21: With TimeSerial(), you have access to separate parts of the time.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>22: Terminate the event procedure.<BR><BR><A NAME="E69E101"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>Computing Retirement</B></FONT></CENTER></H4><BR><P>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.<BR><P><FONT COLOR="#000080"><B>Listing P7.2. Calculating the year of retirement.</B></FONT><BR><PRE><FONT COLOR="#000080">7: ' Filter out the birth year8: BYear = Year(BDate)9: ' Add the number of years to retirement10: RetYear = BYear + 6511: MsgBox "You can retire in" & Str$(RetYear)1: Sub cmdBirth_Click ()2: ' Asks the user for a birthdate and determine retirement3: Dim BDate As Variant4: Dim BYear, RetYear As Integer5: ' Get the birthday6: Do7: BDate = InputBox("When is your birthday (dd/mm/yy)?", "Birthday")8: Loop Until IsDate(BDate) Or BDate = ""9: If BDate <> "" Then ' If not Cancel10: ' Filter out the birth year11: BYear = Year(BDate)12: ' Add the number of years to retirement13: RetYear = BYear + 6514: MsgBox "You can retire in" & Str$(RetYear)15: End If16: End Sub</FONT></PRE><BR><A NAME="E69E102"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>Descriptions</B></FONT></CENTER></H4><BR><P>1: The command button's Name property contains cmdBirth, so the name of the Click event procedure is cmdBirth_Click().<BR><P>2: A remark explains the purpose of the procedure.<BR><P>3: Define a variant variable that will hold the user's birthdate.<BR><P>4: Define two integer variables. One will hold the user's birth year and the other will hold the year of retirement.<BR><P>5: A remark that explains the purpose of the subsequent code.<BR><P>6: Begin a loop to get a valid date.<BR><P>7: Ask the user for his or her birthdate.<BR><P>8: Keep looping until the user enters a valid date or until the user presses Cancel.<BR><P>9: Perform retirement routine on date as long as long as the user doesn't press Cancel.<BR><P>10: A remark that explains the purpose of the subsequent code.<BR><P>11: Strip off the year of the birthdate and save the year.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE>11: Year() pulls the year from a date value.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>12: A remark that explains the purpose of the subsequent code.<BR><P>13: Add 65 to the birth year to find the year of retirement.<BR><P>14: Display the retirement message.<BR><P>15: Terminate the If statement.<BR><P>16: Terminate the event procedure.<BR><BR><A NAME="E69E103"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>Close the Application</B></FONT></CENTER></H4><BR><P>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.<P ALIGN=LEFT><A HREF="vel14.htm" TARGET="_self"><IMG SRC="purprev.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Previous Page"></A><A HREF="#I0" TARGET="_self"><IMG SRC="purtop.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Page Top"></A><A HREF="index.htm" TARGET="_self"><IMG SRC="purtoc.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="TOC"></A><A HREF="vel15.htm" TARGET="_self"><IMG SRC="purnext.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Next Page"></A></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -