JavaScript Editor js editor     Web development 



Main Page

File: ...\Samples\Solution\Controls\Timer\Swatch.scx

This sample includes the Stopwatch class.

Stopwatch Class

The Stopwatch class includes a timer and several display labels. The Timer increments numeric custom properties of the class and sets the Caption property of the labels accordingly.

The Start method of the class sets the Enabled property of the Timer to true (.T.). The Stop method sets the Enabled property of the Timer to false (.F.). And the Reset method sets the numeric properties to 0.

Property Settings for the Stopwatch Class

Control Property Setting

lblSeconds

Caption

00

lblColon1

Caption

:

lblMinutes

Caption

00

lblColon2

Caption

:

lblHours

Caption

00

tmrSWatch

Interval

1000

Swatch Form

The Swatch form contains an object derived from the Stopwatch class, along with some command buttons. The code written for the Click event of the first command button on the form calls the Start and Stop methods of the Stopwatch class. The second button calls the Reset method of the Stopwatch class.

Protected Properties and Methods

This sample also illustrates the use of protected properties and methods. The Stopwatch class contains three protected properties, nSec, nMin, and nHour, and one protected method, UpdateDisplay.

Tip:
Choose Class Info on the Class menu to see the visibility of all properties and methods of a class.

The protected properties are used in internal calculations in the UpdateDisplay method and the Timer event. The UpdateDisplay method sets the captions of the labels to reflect the elapsed time.

UpdateDisplay Method

Code Comments
В Copy Code
cSecDisplay = ALLTRIM(STR(THIS.nSec))
cMinDisplay = ALLTRIM(STR(THIS.nMin))
cHourDisplay = ALLTRIM(STR(THIS.nHour))

Convert the numeric properties to Character type for display in the label captions.

В Copy Code
THIS.lblSeconds.Caption = ;
 IIF(THIS.nSec < 10, ;
   "0" ,"") + cSecDisplay
THIS.lblMinutes.Caption = ;
  IIF(THIS.nMin < 10, ;
   "0", "") + cMinDisplay
THIS.lblHours.Caption = ;
  IIF(THIS.nHour < 10, ;
   "0", "") + cHourDisplay

Set the label captions, retaining the leading 0 if the value of the numeric property is less than 10.

The following table lists the code in the tmrSWatch.Timer event:

The Timer Event

Code Comments
В Copy Code
THIS.Parent.nSec = THIS.Parent.nSec + 1
IF THIS.Parent.nSec = 60
  THIS.Parent.nSec = 0
  THIS.Parent.nMin = ;
  THIS.Parent.nMin + 1
ENDIF

Increment the nSec property every time the timer event fires: every second. If nSec has reached 60, reset it to 0 and increment the nMin property.

В Copy Code
IF THIS.Parent.nMin = 60
  THIS.Parent.nMin = 0
  THIS.Parent.nHour = ;
  THIS.Parent.nHour + 1
ENDIF
THIS.Parent.UpdateDisplay

If nMin has reached 60, reset it to 0 and increment the nHour property.

Call the UpdateDisplay method when the new property values are set.

The Stopwatch class has three custom methods that are not protected: Start, Stop, and Reset. A user can call these methods directly to control the stopwatch.

The Start method contains the following line of code:

В Copy Code
THIS.tmrSWatch.Enabled = .T.

The Stop method contains the following line of code:

В Copy Code
THIS.tmrSWatch.Enabled = .F.

The Reset method sets the protected properties to zero and calls the protected method:

В Copy Code
THIS.nSec = 0
THIS.nMin = 0
THIS.nHour = 0
THIS.UpdateDisplay

The user cannot directly set these properties or call this method, but code in the Reset method can.

See Also



JavaScript Editor js editor     Web development