C#. Windows Forms. Controlling the displaying of the current time in the program. Example of demonstration of Timer component

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


Search other websites:

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.

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.

Microsoft Visual Studio represents the control Timer, which realizes execution of the interrupt from timer.

Also, using the Timer it is possible to organize a parallel execution of the different processes in the program.

In the given topic is shown, how to use Timer for output the current time on the form. Also, is shown how you can control the timer, by stopping and continuing the view of current time.



1. Create a new project as Windows Forms Application

Run MS Visual Studio. Create the project as Windows Forms Application. The detailed example of creating a Windows Forms Application is described here.


2. Creating the main form

Create the form as shown in Figure 1.

Place on the form following controls:

  • the control of Label type. The object (class instance) named label1 is created;
  • two controls of Button type. Two objects with names button1 and button2 are created;
  • the control of Timer type (Figure 1). The object named timer1 is created.

C# Windows Forms Application Placement control Timer

Figure 1. Placing on the form the controls Timer and others


3. Setting up the controls of types Label and Button

Set up following properties of controls Label and Button:

  • in the control label1 property Text = ” ‘;
  • in the control button1 property Text = “Go“;
  • in the control button2 property Text = “Stop“.

If user clicks on the button button1 (Start), this enables the timer. When users clicks on the button2, it disables the timer.

After setting up of controls, the main form will have view as shown in Figure 2.

C# Windows Forms application formFigure 2. The application form after setting up the controls


4. Setting up the control of Timer type

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

C# Windows Forms Property Interval control TimerFigure 3. Property Interval of control timer1


5. Programming the event handler Tick

In the event handler Tick you need to write your own program code, which displays the current day time.

The code listing of event handler timer1_Tick() of event Tick is following:

private void timer1_Tick(object sender, EventArgs e)
    label1.Text = DateTime.Now.ToString();

In the code above are used the possibilities of class DateTime, which has the means of displaying the current time.


6. Programming the events of click on the buttons button1 and button2

To display the time you need to activate the timer. To activate/deactivate timer in the timer1 control is used the Enabled property.

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

private void button1_Click(object sender, EventArgs e)
    timer1.Enabled = true;

To stop the timer you need the Enabled property of control timer1 set to value false.

private void button2_Click(object sender, EventArgs e)
    timer1.Enabled = false;


7. Run the application

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

C# Windows Forms Program executing mode

Figure 4. Program in the executing mode

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

timer1.Enabled = true;

place at the begin of loading the form (into the constructor of into the event handler Load).