Home

Windows Controls: The Timer

   

Introduction to the Timer

 

Description

A timer is a non-spatial object that uses recurring lapses of time in a computer or in an 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.

Practical LearningPractical Learning: Introducing the Timer Control

  1. Start Embarcadero RAD Studio
  2. To create a new application, on the main menu, click File -> New -> VCL Forms Application - C++Builder
  3. In the Object Inspector, change the properties of the form as follows:
    Caption: Traffic Light
    BorderStyle: bsToolWindow
    Name: frmTraffic
  4. To save the project, on the Standard toolbar, click the Save All button
  5. Click the New Folder button
  6. Type TrafficLight1 as the name of the folder and press Enter twice to display the contents of the folder
  7. Change the name of the unit to Traffic and click Save
  8. Change the name of the project to TrafficLight and click Save
  9. Press F9 to test the application
  10. Close the form and return to your programming environment
  11. Copy the following pictures and paste them in the Debug sub-folder of the current project:
     
    Red Light Orange Light
    Green Light
  12. Design the form as follows:
     
    Traffic Light
    Control AutoSize Name Picture
    TImage TImage True imgLight RedLight.bmp
  13. Under the Code Editor, click Traffic.h
  14. Create an enumeration named CurrentLight and declare a variable for it named Light:
    //---------------------------------------------------------------------------
    
    #ifndef TrafficH
    #define TrafficH
    //---------------------------------------------------------------------------
    #include <Classes.hpp>
    #include <Controls.hpp>
    #include <StdCtrls.hpp>
    #include <Forms.hpp>
    #include <ExtCtrls.hpp>
    #include <Graphics.hpp>
    //---------------------------------------------------------------------------
    enum CurrentLight { Red, Orange, Green };
    //---------------------------------------------------------------------------
    class TfrmTraffic : public TForm
    {
    __published:	// IDE-managed Components
    	TImage *imgLight;
    private:	// User declarations
    	CurrentLight Light;
    public:		// User declarations
    	__fastcall TfrmTraffic(TComponent* Owner);
    };
    //---------------------------------------------------------------------------
    extern PACKAGE TfrmTraffic *frmTraffic;
    //---------------------------------------------------------------------------
    #endif
  15. Press F12 to display the form
  16. In the Structure window, click frmTraffic
  17. In the Object Inspector, click Events
  18. Double-click the right box of OnCreate
  19. Implement the event as follows:
    //---------------------------------------------------------------------------
    void __fastcall TfrmTraffic::FormCreate(TObject *Sender)
    {
    	Light = CurrentLight::Green;
    }
    //---------------------------------------------------------------------------
  20. Press F12 to display the form
  21. In the Object Inspector, click Properties

Creating a Timer

The timer in VCL applications is made available through the TTimer class. The TTimer class is (directly) derived from TComponent:

TTimer Inheritance

To add it to your application at design time, from the System property page of the Tool Palette, click Timer and click on the form.

 
 
 

Creating a Timer

The timer in VCL applications is made available through the TTimer class. The TTimer class is (directly) derived from TComponent:

TTimer Inheritance

To add it to your application at design time, from the System property page of the Tool Palette, click Timer and click on the form.

Characteristics of a Timer

 

The Interval

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 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.

Enabling a Timer

One of the ways you can use a timer 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 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 LearningPractical Learning: Using Timer Controls

  1. On the Tool Palette, click System
  2. Click TTimer 
  3. Click the form
  4. On the form, double-click the TTimer1 control to access its OnTimer event
  5. Change its code as follows:
    //---------------------------------------------------------------------------
    void __fastcall TfrmTraffic::Timer1Timer(TObject *Sender)
    {
    	// If the current color is red
    	if( Light == CurrentLight::Red )
    	{
    		// Change the amount of time to 1500 seconds
    		Timer1->Interval = 1500;
    		// Change the color to orange
    		imgLight->Picture->LoadFromFile(L"OrangeLight.bmp");
    		// Update the light tag
    		Light = CurrentLight::Orange;
    	}
    	// But if the color is orange
    	else if( Light == CurrentLight::Orange )
    	{
    		// Change the amount of time
    		Timer1->Interval = 7500;
    		// Change the color to green
    		imgLight->Picture->LoadFromFile(L"GreenLight.bmp");
    		// Update the light sentinel
    		Light = CurrentLight::Green;
    	}
    	// Otherwise, if the color is green
    	else //  if( Light == CurrentLight::Green )
    	{
    		// Change the amount of time
    		Timer1->Interval = 5000;
    		// Change the color to red
    		imgLight->Picture->LoadFromFile(L"RedLight.bmp");
    		// Update the light tag
    		Light = CurrentLight::Red;
    	}
    }
    //---------------------------------------------------------------------------
  6. Save all
  7. Press F9 to test the program
     
    Traffic Light Traffic Light
    Traffic Light
 
 
   
 

Home Copyright © 2010-2016, FunctionX