⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vel14.htm

📁 简单的说明如何使用VB,非常适合初学使用者,而且是用图表来解说的
💻 HTM
📖 第 1 页 / 共 3 页
字号:
<FONT COLOR="#000080"><B>Listing 14.2. Letting the user change the date and time.</B></FONT><BR><PRE><FONT COLOR="#000080">1: Dim newDate As Variant2: Dim newTime As Variant3: MsgBox &quot;The current date is &quot; &amp; Date$ ' Calls function4: newDate = InputBox(&quot;What do you want to set the date to?&quot;)5: If IsDate(newDate) Then6: Date$ = newDate7: End If ' Don't do anything if a good date isn't entered8: MsgBox &quot;The date is now &quot; &amp; Date$9: MsgBox &quot;The current time is &quot; &amp; Time$ ' Calls function10: newTime = InputBox(&quot;What do you want to set the time to?&quot;)11: If IsDate(newTime) Then12: Time$ = newTime13: End If ' Don't do anything if a good time isn't entered14: MsgBox &quot;The time is now &quot; &amp; Time$</FONT></PRE><P><FONT COLOR="#FF8000"><B><I>Analysis: </I></B></FONT>After defining two variant variables in lines 1 and 2 to hold the date and time values entered by the user, the program displays the date in line 3 and asks the user to change the date in line 4. Line 5 uses the IsDate() function, which you learned about in the previous unit, to check that the user entered a proper date value. If the user didn't enter a valid date, the program skips line 6 and displays the current set date without change in line 8.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Note: </B>Visual Basic won't allow <I>any</I> invalid date to slip through. If the user were to enter 2-30-96, the IsDate() function in line 5 would know that February doesn't have 30 days and would not consider the date to be valid.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>Lines 9 through 14 perform the same routine for the time.<BR><BR><A NAME="E68E107"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>How Much Time Has Passed?</B></FONT></CENTER></H3><BR><P><FONT COLOR="#FF8000"><B><I>Concept: </I></B></FONT>Visual Basic includes a Timer() function that enables you to compute the elapsed time between two time periods. Using Timer(), you can find out how many seconds have elapsed since midnight.<BR><P>The Timer() function returns the number of seconds that have elapsed since your computer's clock was last midnight. Timer() requires no arguments; hence, Visual Basic removes Timer()'s parentheses if you type them, but this book uses the parentheses, as is the standard, to remind you that Timer() is a function and not a command.<BR><P>Assuming that it's 11:40 p.m., the following statement stores 85206.9 in the variable named TMid:<BR><BR><PRE><FONT COLOR="#000080">TMid = Timer()</FONT></PRE><P>Timer() returns a single-precision value. If you divide 85206.9 by 60, you'll get the number of minutes since midnight, and if you divide by another 60, you'll get the number of hours (a few less than 24) since midnight.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Warning: </B>The Timer() function differs greatly from the timer control on the toolbox that you'll learn about in Lesson 10, &quot;Making Programs Real World.&quot;</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>How can determining the number of seconds since midnight help you as a Visual Basic programmer? Timer() is perfect for timing an event. Suppose that you want to ask the user a question and determine how long it takes the user to answer. First, save the value of Timer() before you ask the user; then, subtract that value from the value of Timer <I>after</I> he or she answers. The difference of the two Timer() values is the number of seconds that the user took to answer.<BR><P><FONT COLOR="#FF8000"><B><I>Stop and Type: </I></B></FONT>Listing 14.3 contains code that asks the user for a math answer and times how long the user takes to respond.<BR><P><FONT COLOR="#FF8000"><B><I>Review: </I></B></FONT>The moment that your program executes the Timer() function, Visual Basic checks the computer's internal clock and returns the number of seconds since midnight.<BR><P><FONT COLOR="#000080"><B>Listing 14.3. Test the user's math speed.</B></FONT><BR><PRE><FONT COLOR="#000080">1: Dim Before, After, timeDiff As Variant2: Dim mathAns As String3: Before = Timer ' Save the time before asking4: Do5: mathAns = InputBox(&quot;What is 150 + 235?&quot;, &quot;Hurry!&quot;)6: Loop Until Val(mathAns) = 2857: After = Timer ' Save the time after answering8: ' The difference between the time values9: ' is how many seconds the user took to answer10: timeDiff = After - Before11: MsgBox &quot;That took you only&quot; + Str$(timeDiff) &amp; &quot; seconds!&quot;</FONT></PRE><P><FONT COLOR="#FF8000"><B><I>Output: </I></B></FONT>Figure 14.1 shows the input box that asks the user for the answer.<BR><P><B> <A HREF="14vel01.gif">Figure 14.1. Test the user's math ability!</A></B><BR><P><FONT COLOR="#FF8000"><B><I>Analysis: </I></B></FONT>Line 3 saves the Timer() setting right before the input box that requests the user's answer appears. The Do loop in lines 4 through 6 then keeps asking the user for the answer until the user enters the appropriate response of 285. As soon as the user enters the correct response, line 7 stores the Timer() value at that point. Line 10 then computes the difference of the two values to determine the number of seconds that the response took.<BR><BR><A NAME="E68E108"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>Serial Dates and Times</B></FONT></CENTER></H3><BR><P><FONT COLOR="#FF8000"><B><I>Concept: </I></B></FONT>Visual Basic supports the storage of serial date and time values. A serial value is a number stored as a VarType 7 (the Date data type, as you learned in the previous unit). The serial number allows you to break up dates into their day, month, and year values and allows you to break up times into their hour, minute, and second values.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><I>Definition: </I>A <I>byte</I> is one character of memory.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>All the date and time functions that you've seen in this unit have been working with serial values. A serial value is the internal representation of a date or time, stored in a VarType 7 or a Variant data type. Visual Basic actually stores these values as double-precision values to ensure the full storage of time and date so that accurate date arithmetic can be performed. Visual Basic uses eight bytes memory of storage for double-precision values, which is enough room to hold the combined time and date value.<BR><P>The following functions are explained in this section:<BR><UL><LI>DateSerial()<BR><BR><LI>DateValue()<BR><BR><LI>TimeSerial()<BR><BR><LI>TimeValue()<BR><BR><LI>Day()<BR><BR><LI>Month()<BR><BR><LI>Year()<BR><BR></UL><P>All of these functions convert their arguments to serial date values. You then can use those serial date values to manage and modify specific parts of date and time values. Here is the format of the DateSerial() function:<BR><BR><PRE><FONT COLOR="#000080">DateSerial(<I>Year</I>, <I>Month</I>, <I>Day</I>)</FONT></PRE><P>where <I>Year</I> is an integer year number (either 00 to 99 for 1900 to 1999, or a four-digit year number) or expression, <I>Month</I> is an integer month number (1 to 12) or expression, and <I>Day</I> is an integer day number (1 to 31) or expression. If you include an expression for any of the integer arguments, you specify the number of years, months, or days from or since a value.<BR><P>These two DateSerial() function calls return the same value:<BR><BR><PRE><FONT COLOR="#000080">d = DateSerial(1990, 10, 6)</FONT></PRE><P>and<BR><BR><PRE><FONT COLOR="#000080">d = DateSerial(1980+10, 12-2, 1+5)</FONT></PRE><P>The DateSerial() function ensures that your date arguments don't go out of bounds. For example, 1992 was a leap year, so February of 1992 had 29 days. However, the following DateSerial() function call appears to produce an invalid date because February, even in leap years, can't have 30 days:<BR><BR><PRE><FONT COLOR="#000080">d = DateSerial(1992, 2, 29+1)</FONT></PRE><P>Nothing is wrong with this function call because DateSerial() adjusts the date evaluated so that d holds March 1, 1992, one day following the last day of February.<BR><P>The DateValue() function is similar to DateSerial() except that the DateValue() function accepts a string argument, as the following format shows:<BR><BR><PRE><FONT COLOR="#000080">DateValue(stringDateExpression)</FONT></PRE><P>The <I>stringDateExpression</I> must be a string that Visual Basic recognizes as a date (such as those for the Date$ statement described earlier in this chapter and valid dates entered by the user). If you ask the user to enter a date one date part value at a time (asking for the year, then the month, and then the day separately), you can use DateSerial() to convert those values to an internal serial date. If you ask the user to enter a full date (that you capture into a string variable) such as <B>July 16, 1996</B>, DateSerial() converts that string to the internal serial format needed for dates.<BR><P>The TimeSerial() and TimeValue() functions work the same as their DateSerial() and DateValue() counterparts. If you have three individual values for a time of day, TimeSerial() converts those values to an internal time format (the Variant or VarType 7). Here is the format of TimeSerial():<BR><BR><PRE><FONT COLOR="#000080">TimeSerial(<I>Hour</I>, <I>Minute</I>, <I>Second</I>)</FONT></PRE><P>The TimeSerial() function accepts expressions for any of its arguments and adjusts those expressions as needed, just as the DateSerial() function does.<BR><P>If you have a string with a time value (maybe the user entered the time), the TimeValue() function converts that string to a time value with this format:<BR><BR><PRE><FONT COLOR="#000080">TimeValue(stringTimeExpression)</FONT></PRE><P>The Day(), Month(), and Year() functions convert their date arguments (of Variant or the VarType 7 data type) to a day number, month number, or year number. These three functions are simple. Here are their formats:<BR><PRE><FONT COLOR="#000080">Day(DateArgument)Month(DateArgument)Year(DateArgument)</FONT></PRE><P>You often pass today's date (found with Now()) to the Day(), Month(), and Year() functions as shown here:<BR><PRE><FONT COLOR="#000080">d = Day(Now())m = Month(Now())y = Year(Now())</FONT></PRE><P>The current date's day of week number, month number, and year are stored in the three variables d, m, and y, respectively.<BR><P><FONT COLOR="#FF8000"><B><I>Stop and Type: </I></B></FONT>Listing 14.4 contains a short section of code that asks the user for a date, uses the IsDate() function to ensure that the date is proper, and then uses the Month(), Day(), and Year() functions described here to break the date into its individual parts.<BR><P><FONT COLOR="#FF8000"><B><I>Review: </I></B></FONT>The serial date value that Visual Basic uses for all date and time values enable you to use various functions to break apart and piece back together various date and time combinations.<BR><P><FONT COLOR="#000080"><B>Listing 14.4. Working with serial date values.</B></FONT><BR><PRE><FONT COLOR="#000080">1: Dim UserDate As Variant2: Dim DStr As String3:4: ' Ask the user for a date5: Do6: UserDate = InputBox(&quot;What's the date?&quot;)7: Loop While Not IsDate(UserDate)8:9: DStr = &quot;Month: &quot; &amp; Month(UserDate)10: DStr = DStr + &quot;, Day: &quot; &amp; Day(UserDate)11: DStr = DStr + &quot;, Year: &quot; &amp; Year(UserDate)12:13: ' Display the date broken down14: MsgBox DStr</FONT></PRE><P><FONT COLOR="#FF8000"><B><I>Output: </I></B></FONT>Assuming that the user types <B>1/2/98</B> for the date in response to this code's InputBox() function call, Figure 14.2 contains the message box that shows the various parts of the dates.<BR><P><B> <A HREF="14vel02.gif">Figure 14.2. Through functions, you can break up a date.</A></B><BR><P><FONT COLOR="#FF8000"><B><I>Review: </I></B></FONT>Lines 1 and 2 define two variables needed by the rest of the program. The variant UserDate variable will hold a date value entered by the user, and the DStr variable will hold a message box string.<BR><P>Lines 5 though 7 loop until the user enters an appropriate date value. The loop continues as long as IsDate() reveals that the user's date value is invalid. The loop quits asking the user for a date when line 7 determines that the date is valid.<BR><P>Lines 9 through 11 build a fairly complicated string displayed by line 14's message box function. The lines concatenate several strings together, building upon the DStr variable. The Month(), Day(), and Year() functions all pick off the month, day, and year values from the date entered by the user to display the different values inside the message box, as shown in Figure 14.2.<BR><BR><A NAME="E68E109"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>Formatting with </B><B>Format()</B></FONT></CENTER></H3><BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><I>Definition: </I>A <I>logical</I> value is the result of a relation or a True or False control value.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P><FONT COLOR="#FF8000"><B><I>Concept: </I></B></FONT>The Format() function formats numeric and logical values so that you can display that data in the exact format that you require. Until this section, you couldn't ensure that currency values displayed to two decimal places, but Format() enables you to format currency and all other numbers the way you need.<BR><P>Visual Basic can't read your mind, so it doesn't know how you want numbers displayed in your applications. Although Visual Basic sometimes displays none, one, or two decimal places for currency values, you'll almost always want those currency values displayed to two decimal places.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Note: </B>As with the date and time functions, if you've set your computer's international settings to a country other than the U.S.'s (as done for this book), your formatted currency values may differ from those shown here. Some countries swap the use of commas and decimal places from those used in the U.S.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>The two Format() functions are Format$() and Format() and they differ only in their return data types. Format$() returns a string and Format() returns a variant data type. Here is the format of Format():<BR><BR>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -