vel20.htm

来自「简单的说明如何使用VB,非常适合初学使用者,而且是用图表来解说的」· HTM 代码 · 共 1,134 行 · 第 1/2 页

HTM
1,134
字号
<BR><P>Lines 8 through 10 check the check mark box to see whether the user wants the second beep or not. If the check box is checked, and therefore its Values property will be True, line 9 executes.<BR><P>Line 12 then updates the value of the large clock label in the center of the form. The Time$ function accesses the computer's clock every second because this event procedure executes every second.<BR><P>Figure 20.5 shows CLOCK.MAK's Form window during the program's development. Notice the timer control sitting in the lower-left corner of the screen. This timer control doesn't appear when the user runs the program, but the timer control's involvement in the program is crucial because the timer control triggers the only event procedure in the entire application.<BR><P><B> <A HREF="20vel05.gif">Figure 20.5. The timer control doesn't display in the running program.</A></B><BR><BR><A NAME="E68E151"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>Events That Occur Infrequently</B></FONT></CENTER></H3><BR><P><FONT COLOR="#FF8000"><B><I>Concept: </I></B></FONT>The Interval property holds a maximum value of 65535. 65535 allows for a time interval of only a little more than a minute. If you write timer events that need to execute less frequently than every minute, you need to add code that keeps the timer event from running every time the timer event executes.<BR><P>Suppose, for example, that you wanted to warn the user to select File Save every five minutes. Anytime that you need something to happen once each time period, whatever that time period may be, use the timer control. To display the File Save message box every five minutes, you could do the following:<BR><OL><LI>Place a timer control on the application's form.<BR><BR><LI>Determine how many milliseconds there are in five minutes. There are 1,000 milliseconds in each second. There are 60,000 milliseconds in each minute. Therefore, there are 300,000 milliseconds every five minutes.<BR><BR><LI>The number of milliseconds exceeds 65,535 (the maximum allowable value for the Interval property) so you'll have to divide the time period into more manageable units of milliseconds, seconds, or one minute intervals.<BR>Five minutes is easily divided into five one-minute time intervals. One minute is equal to 60,000 milliseconds. Therefore, you'll have to set the Interval property to 60000 and control the File Save message box display so that the message box appears only once out of every five times that the event procedure executes.<BR><BR></OL><P><FONT COLOR="#FF8000"><B><I>Stop and Type: </I></B></FONT>Listing 20.2 contains a timer event procedure that executes a message box statement every five minutes. The timer event executes every minute, owing to the timer's Interval value of 60000. One simple way to accomplish displaying the message box every five minutes is to define a module variable named MinCount that counts the minutes that go by and executes the message box statement every time that MinCount reaches 5. Notice that, at the top of Listing 20.2, you'll find the (general) declarations procedure that defines a module-level variable named MinCount. MinCount won't lose its former value between event procedure executions because MinCount is a module variable and isn't locally defined inside tmrMinute_Timer().<BR><P><FONT COLOR="#FF8000"><B><I>Review: </I></B></FONT>When you need to execute code that must occur in time intervals greater than milliseconds, seconds, or one-minute intervals, define a module-level variable to keep track of multiple occurrences of second or minute time intervals.<BR><P><FONT COLOR="#000080"><B>Listing 20.2. Executing a timer event every five minutes.</B></FONT><BR><PRE><FONT COLOR="#000080">1: ' Inside the (general) procedure2: ' Define a module variable to keep3: ' track of one-minute time intervals4: Option Explicit5: Dim MinCount As Integer ' Begins at zero6:7: Sub tmrMinute_Timer ()8: ' Ensures that a message box appears only every9: ' five minutes despite the fact that this10: ' event procedure executes every passing minute11: MinCount = MinCount + 1 ' Raise minute count12: If (MinCount = 5) Then13: MsgBox &quot;It's time to save your file!&quot;14: MinCount = 0 ' Reset the minute count15: End If16: End Sub</FONT></PRE><P><FONT COLOR="#FF8000"><B><I>Analysis: </I></B></FONT>Line 5 defines the module variable named MinCount. All defined variables begin with initial values of zero. Remember that the timer named tmrMinute that executes the Timer subroutine procedure in this example would contain an Interval value of 60000, so the tmrMinute_Timer() event procedure executes every minute that goes by.<BR><P>Once the minute count reaches 5, five minutes will have gone by since the MinCount variable was defined or since the last time that this event procedure executed. Therefore, line 13 displays the message box and line 14 resets the minute count to zero again so that the counting towards five minutes will begin to happen again. If, however, the minute count has not reached 5, the If condition is false and the subroutine terminates without displaying the message box.<BR><BR><A NAME="E68E152"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>The Timer Is Not an Alarm</B></FONT></CENTER></H3><BR><P>You can't set a timer control to trigger an event at a specific single period in time. The timer control does <I>not</I> work like an alarm that goes off at a specific point in time.<BR><P><FONT COLOR="#FF8000"><B><I>Concept: </I></B></FONT>The timer control executes its Timer event procedure every time that the defined Interval passes. The timer control doesn't act as an alarm, however, executing an event procedure at a preset time. You can program the Timer event procedure, however, to simulate an alarm clock or a calendar reminder.<BR><P>The timer control triggers a repeated event over and over. When you write a timer event procedure, the code inside the procedure must be able to handle its own execution every time an event occurs. If you <I>do</I> want to execute a routine once at a preset time, you'll have to write the event procedure to execute repeatedly (once each preset time interval); the code then must check to see whether the PC's time equals that of the preset time that you're waiting on.<BR><P><FONT COLOR="#FF8000"><B><I>Stop and Type: </I></B></FONT>Listing 20.3 contains the code needed to turn CLOCK.MAK into a wake-up alarm program. You can find the alarm code on this book's disk under the project filename ALARM.MAK file. When you run the program, you'll see an extra command button labeled Set Alarm. When you click Set Alarm, an input box appears asking for a time to wake up. When you enter a correct time (the program loops until the you enter a correct time or choose Cancel), the set alarm time appears to the right of the command button as shown in Figure 20.6 and a message box alarm goes off when that time is reached.<BR><P><FONT COLOR="#FF8000"><B><I>Review: </I></B></FONT>When writing an alarm or calendar program, remember that the Timer event will always execute continuously, at least every 65,535 milliseconds because 65535 is the largest value that you can store in a timer control's Interval property. When the Timer event executes, your code can check the current time (or date if you write a date-checking calendar program) to see whether an alarm time has been reached.<BR><P><FONT COLOR="#000080"><B>Listing 20.3. Code that sets an alarm and waits to wake up.</B></FONT><BR><PRE><FONT COLOR="#000080">1: ' Define a module variable used in two2: ' different procedures. The module variable3: ' cannot lose its value between runs.4: Option Explicit5: Dim TimeSet As Variant6:7: Sub cmdSet_Click ()8: ' Ask the user for an alarm time to set9: ' Keep looping until the user:10: ' Enters a valid time or11: ' clicks the Cancel command button12: Do13: TimeSet = InputBox(&quot;Enter a 24-hour time to wake up&quot;, &quot;Time&quot;)14: If (TimeSet = &quot;&quot;) Then ' Check for Cancel15: Exit Sub ' User pressed Cancel16: End If17: Loop Until IsDate(TimeSet) ' Keep asking if invalid18: ' If here, the user entered a value that can19: ' be converted to a valid time value. Now,20: ' convert the Variant data to a date/time type21: TimeSet = CVDate(TimeSet)22: ' Display the wake-up time in a label23: lblSet.Caption = &quot;Alarm @ &quot; &amp; TimeSet24: End Sub25:26: Sub tmrClock_Timer ()27: ' Sound an alarm if the set time has been hit28: ' just now or perhaps bypassed by a fraction29: Dim ctr As Integer30:31: If (chkBeep.Value) Then ' Second beep if needed32: Beep33: End If34: lblClock.Caption = Time$ ' Update the screen's clock35:36: ' Sound the alarm if the wake up time has been37: ' passed (also, make sure a time has been set)38: If (Time &gt;= TimeSet) And (lblSet.Caption &lt;&gt; &quot;No alarm&quot;) Then39: For ctr = 1 To 5040: Beep ' Wake up!!!41: Next ctr42: MsgBox &quot;W-a-k-e U-p-!-!-!&quot;, MB_ICONEXCLAMATION, &quot;Alarm&quot;43: lblSet.Caption = &quot;No alarm&quot; ' To keep alarm from resounding44: End If45: End Sub</FONT></PRE><P><FONT COLOR="#FF8000"><B><I>Output: </I></B></FONT>Figure 20.6 shows what happens when the user sets a time that's about to trigger an alarm.<BR><P><B> <A HREF="20vel06.gif">Figure 20.6. An alarm is ready to go off.</A></B><BR><P>When the alarm sounds, the user hears a wake-up series of beeps and sees the message box shown in Figure 20.7.<BR><P><B> <A HREF="20vel07.gif">Figure 20.7. It's time to wake the user.</A></B><BR><P><FONT COLOR="#FF8000"><B><I>Analysis: </I></B></FONT>Line 1 through 5 contains the (general) code that defines a module variable named TimeSet that will hold the user's wake-up time. The reason that you often must define module variables when dealing with the Timer event, instead of using an argument list to pass values, is because Visual Basic generates the Timer event and there is no way to request that Visual Basic generate a Timer event procedure that receives or sends arguments. Therefore, any variable that you use inside a Timer event procedure must be a module or global variable if you want to initialize that variable elsewhere and use that variable inside the Timer procedure.<BR><P>Line 7 begins the procedure that requests the wake-up alarm time from the user. The subroutine contains a loop in lines 12 through 17 that asks the user for a time, and keeps asking until the user enters a valid time or until the user presses the Cancel command button.<BR><P>Line 21 then converts the user's variant variable that contains a valid time (thanks to the IsDate() function call on line 17, which ensured that the user entered a valid time) to a date format that you can use to compare against the Time$() function in the Timer() event procedure later. Line 23 changes the default caption of No alarm next to the Set Alarm command button to the newly set wake-up time.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Caution: </B>If the user enters only a date with no time, line 17's IfDate() function passes the date along. There is no way to check for an individual time using a built-in function.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>Line 26 begins the Timer event procedure. Lines 31 through 33 check to see whether the second beeping should continue (the user would have selected the check box on the form if the beep is desired). Line 34 updates the large clock display on the screen every second without fail.<BR><P>Line 38 checks to see whether it's time for the alarm to sound. Owing to imperfections in Windows, it's possible for the Timer event procedure to skip a second once in a while, perhaps because the user switched to another Windows program temporarily, which sometimes causes a timer control to miss a beat. Therefore, line 38 checks to see whether the current time is later than or equal to the set time using the &gt;= operator. Also, line 38 checks to see whether an alarm is even set.<BR><P>If the alarm time has been reached and an alarm is set, a For loop in lines 39 through 41 beeps the speaker for a series of 50 beeps, and then line 42 displays a message box to awaken the user. Line 43 resets the caption now that an alarm just sounded, so the program won't keep sounding the alarm every subsequent second that follows.<BR><BR><A NAME="E68E153"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>Homework</B></FONT></CENTER></H3><BR><BR><A NAME="E69E135"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>General Knowledge</B></FONT></CENTER></H4><BR><OL><LI>What control keeps track of elapsed time?<BR><BR><LI>What is the most important property of the timer control?<BR><BR><LI>Why doesn't the timer control contain any color or size properties?<BR><BR><LI>What does the user see in the upper-left corner of a executing program's form when you place a timer control in the upper-left corner of a form?<BR><BR><LI>True or false: The user triggers timer events.<BR><BR><LI>What time value is measured by the timer control's Interval property?<BR><BR><LI>True or false: An application can have, at most, one timer control.<BR><BR><LI>What is a <I>millisecond</I>?<BR><BR><LI>How much time does ten thousand milliseconds measure?<BR><BR><LI>How much time does sixty thousand milliseconds measure?<BR><BR><LI>What Name prefix do programmers usually assign to the timer control?<BR><BR><LI>What problem can you run into if you attempt to set a timer's Interval property with an integer variable?<BR><BR><LI>What is the name of the only event procedure available for time controls?<BR><BR><LI>True or false: You can set the timer's Interval property to trigger an event at a certain preset time.<BR><BR><LI>Why would Visual Basic sometimes be error prone if you were to test the Time$() value against an exact time match?<BR><BR></OL><BR><A NAME="E69E136"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>Write Code That...</B></FONT></CENTER></H4><BR><OL><LI>Modify the code in <A HREF="velp07.htm">project 7</A> so that the time display updates every second. The update should occur no matter which time zone the user selects.<BR><BR></OL><BR><A NAME="E69E137"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>Extra Credit</B></FONT></CENTER></H4><BR><P>Write an application with three command buttons labeled 10 Seconds, 30 Seconds, 45 Seconds. When the user clicks one of the command buttons, display a seconds countdown for the appropriate number of seconds. When the countdown finishes, beep five times to tell the user that the countdown completed. You can use either three separate timer controls or modify one timer's interval value depending on the command button selected by the user.<BR><P ALIGN=LEFT><A HREF="vel19.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="velp10.htm" TARGET="_self"><IMG SRC="purnext.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Next Page"></A><A </BODY></HTML>

⌨️ 快捷键说明

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