The Timer Control

 

Introduction

A timer is a non-spatial object that uses recurring lapses of time in a computer or in your application. To work, every lapse of period, the control sends a message to the operating system. The message is something to the effect of "I have counted the number of lapses you asked me to count".

As opposed to the time that controls your computer, a timer is partly but greatly under your control. Users do not see nor do they use a timer as a control. As a programmer, you decide if, why, when, and how to use this control.

Timer Example: Traffic Light

Practical Learning Practical Learning: Introducing the Timer Control

  1. Start a new application with the default form
  2. Save it in a new folder named TrafficLight1
  3. Save the unit as Exercise and save the project as TrafficLight
  4. Change the Caption of the form to Traffic Light
  5. Change its BorderStyle property to bsDialog
  6. Change its dimensions to Height = 256 and Width = 112
  7. From the Statndard tab of the Component Palette, double-click the Panel control
  8. Change the properties of the panel as follows:
    BevelInner = bvNone
    BevelOuter = bvLowered
    BevelWidth = 5
    Caption = Empty
    Color = clBlack
    Height = 209
    Left = 17
    Top = 8
    Width = 81
  9. While the panel is still selected on the form, from the Additional tab of the Component Palette, double-click the Shape control
  10. Change the following properties for the new Shape:
    Brush: Color = clGray
    Brush: Style = bsSolid
    Height = 65
    Left = 8
    Top = 8
    Width = 65
  11. On the panel, click the newly added Shape to make sure it is selected. Press Ctrl + C to copy it to the clipboard
  12. Click anywhere on the panel to select it
  13. Press Ctrl + V to paste the Shape
  14. While the new Shape is still selected, change its Left value to 8 and its Top value to 72
  15. Click an empty area on the panel to select it and press Ctrl + V
  16. While the new shape is still selected, change its Left value to 8 and its Top value to 136
  17. Click the panel on the form and ress Ctrl + V again
  18. Change the properties of the new shape as follows:
    Brush: Color = clRed
    Height = 57
    Left = 8
    Name = shpRed
    Shape = stCircle
    Top = 12
  19. On the form, click the red circle and press Ctrl + C. Then press Ctrl + V to add a new Shape
  20. Change its properties as follows:
    Brush: Color = clSilver
    Height = 57
    Left = 8
    Name = shpYellow
    Shape = stCircle
    Top = 76
  21. On the form, click the Panel and press Ctrl + V
  22. Change the properties of the new Shape to:
    Brush: Color = clSilver
    Height = 57
    Left = 8
    Name = shpGreen
    Shape = stCircle
    Top = 140
  23. Save All

Characteristics of a Timer

The timer in VCL applications is made available through the TTimer class. To add it to your application at design time, from the System property page of the Component Palette, click Timer and click on the form.

The Interval

The Timer control has two properties that are particularly important for its functionality. A timer is an object used to count lapses of time and send a message when it has finished counting. The amount of time allocated for counting is called an interval. The Interval is probably the most important characteristic of the Timer control because it measures and controls the total time needed to perform a complete count. The Interval is measured in milliseconds. Like any counter, the lower the value, the faster the count will finish, and the higher the value, the longer the count (if you ask one kid to count from 1 to 10 and you ask another to count from 1 to 20 and shout when finished, the first kid would finish first and would shout first). The amount of interval you specify will depend on what you are trying to do.

Enabled

One of the uses you can make of a Timer control is to decide when it should start counting. In some applications, you may want the control to work full-time while in some other applications, you may want the control to work only in response to an intermediate event. The ability to stop and start a Timer control is set using the Enabled Boolean property. When, or as soon as, this property is set to true, the control starts counting. If, when, or as soon as, the Enabled property is set to false, the control stops and resets its counter to 0.

Practical Learning Practical Learning: Using Timer Controls

  1. From the System tab of the Component Palette, double-click Timer 
  2. On the Object Inspector, click the Events tab
  3. Although the dialog box will be equipped with the system Close button, we should provide our own mean of closing the application
  4. On the form, click the red circle
  5. Press and hold Shift, then click the other two circles
  6. On the Object Inspector, double-click the empty area on the right side of OnMouseDown
  7. Change the event as follows:
     
    //---------------------------------------------------------------------------
    void __fastcall TForm1::shpGreenMouseDown(TObject *Sender,
    	TMouseButton Button, TShiftState Shift, int X, int Y)
    {
    	Close();
    }
    //---------------------------------------------------------------------------
  8. Press F12 to display the form
  9. Double-click the Timer on the form to access its OnTimer event
  10. Change its code as follows:
     
    //---------------------------------------------------------------------------
    void __fastcall TForm1::Timer1Timer(TObject *Sender)
    {
    	// If the current color is red
    	if( shpRed->Brush->Color == clRed )
    	{
    		// Change the color to green
    		Timer1->Interval = 3500;
    		shpRed->Brush->Color = clSilver;
    		shpYellow->Brush->Color = clSilver;
    		shpGreen->Brush->Color = clGreen;
    	}
    	// But if the color is green
    	else if( shpGreen->Brush->Color == clGreen )
    	{
    		// Change the color to yellow
    		Timer1->Interval = 2000;
    		shpRed->Brush->Color = clSilver;
    		shpYellow->Brush->Color = clYellow;
    		shpGreen->Brush->Color = clSilver;
    	}
    	// Otherwise, if the color is yellow
    	else // if(shpYellow->Brush->Color == clYellow
    	{
    		// Change the color to red
    		Timer1->Interval = 5000;
    		shpRed->Brush->Color = clRed;
    		shpYellow->Brush->Color = clSilver;
    		shpGreen->Brush->Color = clSilver;
    	}
    }
    //---------------------------------------------------------------------------
  11. Test your program
     
  12. Close it and return to Bcb
  13. Save All

Copyright 2003-2007 FunctionX, Inc.