vel20.htm
来自「简单的说明如何使用VB,非常适合初学使用者,而且是用图表来解说的」· HTM 代码 · 共 1,134 行 · 第 1/2 页
HTM
1,134 行
<HTML><HEAD><TITLE>Visual Basic in 12 Easy Lessons vel20.htm </TITLE><LINK REL="ToC" HREF="index.htm"><LINK REL="Index" HREF="htindex.htm"><LINK REL="Next" HREF="velp10.htm"><LINK REL="Previous" HREF="vel19.htm"></HEAD><BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080"><A NAME="I0"></A><H2>Visual Basic in 12 Easy Lessons vel20.htm</H2><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="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><HR ALIGN=CENTER><P><UL><UL><UL><LI><A HREF="#E68E148" >What You'll Learn</A><LI><A HREF="#E68E149" >Introducing the Timer Control</A><LI><A HREF="#E68E150" >Using the Timer Control</A><LI><A HREF="#E68E151" >Events That Occur Infrequently</A><LI><A HREF="#E68E152" >The Timer Is Not an Alarm</A><LI><A HREF="#E68E153" >Homework</A><UL><LI><A HREF="#E69E135" >General Knowledge</A><LI><A HREF="#E69E136" >Write Code That...</A><LI><A HREF="#E69E137" >Extra Credit</A></UL></UL></UL></UL><HR ALIGN=CENTER><A NAME="E66E29"></A><H1 ALIGN=CENTER><CENTER><FONT SIZE=6 COLOR="#FF0000"><B>Lesson 10, Unit 20</B></FONT></CENTER></H1><BR><A NAME="E67E32"></A><H2 ALIGN=CENTER><CENTER><FONT SIZE=6 COLOR="#FF0000"><B>Time's Up</B></FONT></CENTER></H2><BR><BR><A NAME="E68E148"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>What You'll Learn</B></FONT></CENTER></H3><BR><UL><LI>Introducing the timer control<BR><BR><LI>Using the timer control<BR><BR><LI>Events that occur infrequently<BR><BR><LI>The timer is not an alarm<BR><BR></UL><P>This lesson describes the timer control. Unlike most other controls, the user never sees the timer control, even though you place the timer control on the form just as you place other controls on the form. The timer control keeps track of passing time intervals, and automatically triggers its own events when a preset time period goes by.<BR><P>Unlike the other controls that respond to events, the timer control both triggers events <I>and</I> responds to events through the event procedures that you write. The timer control's property settings determine when the timer control generates events.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Note: </B>The timer control has nothing to do with the Timer() function that you mastered in Lesson 7, "Functions and Dates." The timer control is a control that appears on the Toolbox window, whereas Timer() is a function that you add to your programs inside the Code window.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><BR><A NAME="E68E149"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>Introducing the Timer Control</B></FONT></CENTER></H3><BR><P><FONT COLOR="#FF8000"><B><I>Concept: </I></B></FONT>The timer control sits invisibly on the user's form, waiting for a specified time interval to pass.<BR><P>Figure 20.1 shows where the timer control appears on the toolbox. The timer control looks like a stopwatch. When you place the timer control on the form, the timer control looks like a stopwatch there also. At least, to <I>you</I>, the programmer, the timer control looks like a stopwatch; remember that the user can't see the timer control when the user runs the application.<BR><P><B> <A HREF="20vel01.gif">Figure 20.1. The timer control is the Toolbox window's stopwatch.</A></B><BR><P>You can place the timer control on the form just as you place any other control. You can do the following:<BR><UL><LI>Double-click the timer control so that it appears in the middle of the form. You then can drag the control to the location where you want the control to reside.<BR><BR><LI>Click and drag the timer control to the position on the form where you want the control to reside.<BR><BR></UL><P>When you place the timer control, place the control out of the way of other controls. Keep in mind that the user won't see the timer control, so you can put the timer out of the way to keep it from interfering with the areas of the form where you'll place the other controls.<BR><P>Not only is the placement of the timer control unimportant, but so is its size. No matter how you try to resize the timer control, Visual Basic refuses to change the size. The size of the control doesn't matter anyway because the user won't see the control.<BR><P>Figure 20.2 shows the Form window after you place the timer control in the window's lower-left corner.<BR><P><A NAME="I2"></A><B> <A HREF="20vel02.gif">Figure 20.2. The form with a single timer control.</A></B><BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Note: </B>Any application can have multiple timer controls, just as any application can have multiple instances of the other controls.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>Table 20.1 contains a list of the timer property values available in the Property window for setting up the control. There aren't many timer control properties. If you think about the properties of the other controls, you may remember that most of the properties are designed to aid in the color and size selection of the controls (such as BackColor, Width, and so on). The timer control doesn't need all those extra properties because the user won't see the timer control and those properties would be wasted.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><I>Definition: </I>A <I>millisecond</I> is one thousandth of a second.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><BR><P ALIGN=CENTER><CENTER><FONT COLOR="#000080"><B>Table 20.1. The timer control properties.</B></FONT></CENTER><BR><TABLE BORDERCOLOR=#000040 BORDER=1 CELLSPACING=2 WIDTH="100%" CELLPADDING=2 ><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080><I>Property</I></FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080><I>Description</I></FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Enabled</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Specifies whether the timer control can respond to events.</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Index</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Indicates the subscript of the array if you store the timer control in a control array.</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Interval</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Determines the timer interval, in milliseconds, when you want a timer event to occur. The Interval value can range from 1 to 65535.</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Left</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Specifies the number of twips from the left edge of the Form window where the timer control resides.</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Name</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Contains the name of the control. Unless you change the name, Visual Basic assigns the default names of Timer1, Timer2, and so on as you add timer controls.</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Tag</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Unused by Visual Basic. This is for the programmer's use for an identifying comment applied to the option button.</FONT><TR><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Top</FONT><TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>Specifies the number of twips from the top edge of the Form window where the timer control resides.</FONT></TABLE><P>Generally, the most important timer control property is the Interval property. The other controls are common ones used by most of the other controls, and you're already familiar with most of them. The Interval property is especially important for determining the operation of the timer event procedures that you write for the timer control.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Warning: </B>Remember that the largest positive integer that you can store using the Integer data type is 32767. Therefore, if you initialize the timer's Interval property using a variable, be sure that you use a Long Integer variable or one of the floating-point variables (Single or Double) to hold the initial value.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>Before studying what code to put in timer control event procedures, you need to understand that there is only one event available for the timer control. Not only are there just a handful of timer control properties, there is only a single kind of event procedure that you can write for the timer control.<BR><P>Timer is the event reserved for timer controls. Therefore, if you name a timer control tmrAlarm, the event for that timer control will be called tmrAlarm_Timer(). To open that event, you can double-click the time control that you place on a form, or select the Event from the Code window's Proc dropdown list to open the Timer event procedure.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Note: </B>Visual Basic distinguishes between the Timer event procedure from the built-in Timer() function by the context of how you use the Timer procedure or function.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>Windows imposes a limit on the number of timer controls that you can add to an application at any one time. If you attempt to add more timer controls than Windows allows at the time, Visual Basic displays the error message shown in Figure 20.3. Windows can support only a limited number of timer controls (usually, Windows can handle at least 15 timer controls within a single application). Windows wouldn't be able to keep track of all of the time intervals properly, owing to timing conflicts and slowdowns that occur, if you were to use too many timer controls.<BR><P><B> <A HREF="20vel03.gif">Figure 20.3. The error message that you'll receive if you use too many timers.</A></B><BR><P><FONT COLOR="#FF8000"><B><I>Review: </I></B></FONT>The timer control has few properties and even fewer events. There is no need to specify timer control size and color properties because the user can't see the timer control and these events would be meaningless.<BR><BR><A NAME="E68E150"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>Using the Timer Control</B></FONT></CENTER></H3><BR><P><FONT COLOR="#FF8000"><B><I>Concept: </I></B></FONT>The Interval property determines how much time elapses between timer-generated events. Whereas other controls generate events in response to the user's input, the timer sits in the background, generating events on its own at preselected time intervals.<BR><P>Use the timer control to execute an event procedure at a preselected time interval. When you place a timer control on a form, the timer's Interval property contains the number of milliseconds that pass before the timer's Timer() event procedure executes.<BR><P>Suppose that you place a timer event on the control and set the timer's Interval property to 10000. If you name the timer event tmrSecond, the tmrSecond_Timer() event procedure executes every 10,000 milliseconds, or once every second.<BR><P><FONT COLOR="#FF8000"><B><I>Stop and Type: </I></B></FONT>Figure 20.4 shows the CLOCK.MAK application that you can load and run from this book's companion disks. The CLOCK.MAK application updates the time every second. The option button determines whether you hear a beep every second that the clock updates. The timer control is named tmrClock. Listing 20.1 contains the code for the tmrClock_Timer() event procedure that updates the time on the digital display, and beeps.<BR><P><B> <A HREF="20vel04.gif">Figure 20.4. Watch the time update every second.</A></B><BR><P><FONT COLOR="#FF8000"><B><I>Review: </I></B></FONT>Initialize the Interval property with a value that represents the number of milliseconds that must pass between timer events. The Timer event procedure will then automatically execute every time that the Interval's time passes.<BR><P><FONT COLOR="#000080"><B>Listing 20.1. A digital clock's </B><B>Timer</B><B> event procedure code.</B></FONT><BR><PRE><FONT COLOR="#000080">1: Sub tmrClock_Timer ()2: ' Beep every second if the check box3: ' is set. Also, update the value of the clock4: '5: ' This event procedure executes every second6: ' (or every 1,000 milliseconds) because the7: ' timer control's Interval value is 10008: If (chkBeep.Value) Then9: Beep10: End If ' Don't beep if option button not set11: ' Update the time inside the label12: lblClock.Caption = Time$13: End Sub</FONT></PRE><P><FONT COLOR="#FF8000"><B><I>Analysis: </I></B></FONT>Line 1 shows that the name of the application's timer control is tmrClock. Therefore, that control's single event procedure is named tmrClock_Timer(). The timer control's Interval property is set to 1000 (milliseconds), so tmrClock_Timer() executes once every second.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?