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 |
00 |
|
lblColon1 |
Caption |
: |
lblMinutes |
Caption |
00 |
lblColon2 |
Caption |
: |
lblHours |
Caption |
00 |
tmrSWatch |
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 | ||||
---|---|---|---|---|---|
|
Convert the numeric properties to Character type for display in the label captions. |
||||
|
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 | ||||
---|---|---|---|---|---|
|
Increment the |
||||
|
If Call the |
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.