Controlling the displaying of the current time in the program. Example of demonstration of TTimer component

Controlling the displaying of the current time in the program. Example of demonstration of TTimer component

 

Theoretical information

Sometimes in the application, that is working in the Windows graphic interface, need to display the current time of day (hours : minutes : seconds). To realize this task in the hardware is used timer. Also, using a timer it is possible to realize the parallel execution of tasks using different means.

A programmable timer is designed for program-controlled time delays and generation of functions that specify the time. The programmable timer allows to form the specified intervals regardless of the performance of the computer system. By default, Windows operation system calls the interrupt of timer 18,2 times per second.

Delphi system allows the component TTimer for users. This component implements the interrupt of timer. By using TTimer you can organize a parallel execution in the program.

In the given example is shown how to use the TTimer component for displaying the current time on the form. Also, is shown how you can control the timer, by stopping and continuing the view of current time.

Progress

1. Creating the project as VCL Form Application in Embarcadero RAD Studio – Delphi.

Run Delphi.

Create the project as VCL Form Application.

 

2. Creating of the main form.

Create the form as shown in Figure 1.

Using the palette “Tool Palette” you need to place on the form following components:

  • from tab Standard the component of TLabel type. The object (class instance) named Label1 is created;
  • from the tab Standard two components of type TButton. The two objects with names Button1 and Button2 are created;
  • from the tab System the component of TTimer type (Figure 1). The object with name Timer1 is created.

01_02_00_016_01_

Figure 1. Placing on the form the TTimer component

 

3. Setting up of components of type TLabel and TButton.

Set the following properties of components Label1 and Button1:

  • in the component Label1 property Caption = ““;
  • in the component Button1 property Caption = “Go“;
  • in the component Button2 property Caption = “Stop“.

If user clicks on the button Button1 (Start) the timer will be enabled. When user clicks on the Button2 the timer will be disabled.

At the Figure 2 is displayed the view of form after setting of the components.

01_02_00_016_02_Figure 2. The form of program after setting of the components

 

4. Setting of the component of TTimer type.

In the TTimer component, property Interval set to value 100 (Figure 3) or any other value. The value Interval is measured in the milliseconds. Interval sets the frequency of calling of the event handler. This is the frequency of the changing the value of the current time.

Also, you need set the property Enabled to value False.

01_02_00_016_03_Figure 3. Property Interval of component Timer1

 

5. Programming of the event handler OnTimer.

In the OnTimer event handler is entered your own code, which displays the current time.

Code listing of event handler Timer1Timer() of the event OnTimer is following:

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  Label1.Caption := TimeToStr(Time);
end;

In the code above is used function TimeToStr to convert the current time (property Time) into the string.

 

6. Programming of the events of clicking on the buttons Button1 and Button2.

To display the time you need activate the timer. To activate/deactivate the timer in component Timer1 is used property Enabled.

The event handler of clicking on the button Button1 is following:

procedure TForm1.Button1Click(Sender: TObject);
begin
  Timer1.Enabled := true;
end;

To stop the timer, you need set to “false” the property “Enabled” of the component Timer1.

procedure TForm1.Button2Click(Sender: TObject);
begin
  Timer1.Enabled := false;
end;

 

7. Running the program.

After running the program the form window will be as shown in Figure 4.

01_02_00_016_04_

Figure 4. The program in executing mode

If you need to display the current time then you need place the string

timer1.Enabled := true;

at the beginning of the form loading in the event handler OnActivate.