How to use Timer Control to develop a Stop Watch Application in C#.NET

Filed in ASP.NET, Desktop AppTags: , , , , , , , ,

While developing Window Application in ASP.NET some time we need to use Timer Control for some specific purpose. The Timer control is mainly used to show your output in specific time intervals. Simply we can say that the Timer control is used in that applications where the developer used to show the output after some time intervals. This interval can be a second, a millisecond or else. Today, I will show you to use the Timer control to develop a Stop Watch Application. The Stop Watch is used in many applications now a days.




Free Download

TimerControlInWindowApplication

 



Controls and Properties

To develop a Stop Watch Application you need to use the following controls in your Window Application Form.

  1. Timer control: drag and drop a timer control on the window form and set its “interval” property to 1000 (1sec).
  2. Label control: take a label control to show the time.
  3. Button: Take 3 buttons on the form and set their “text” property to “start”, ”pause”, ”stop”.

 

Namespaces

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

 

Code Page

public partial class Form1 : Form
    {
        int hr, minute, second,mode=0;
        public Form1()
        {
            InitializeComponent();
            btnPause.Enabled = false;
            btnStop.Enabled = false;

        }
        public void initialize()
        {
            label1.Text = "03:00:00";
            hr = 3;
            minute = 0;
            second = 0;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (minute < 0)
            {

                minute = 59;
                hr = hr - 1;
            }
            if (second == 0)
            {

                second = 59;

                if (minute == 0)
                {
                    minute = 59;
                    hr = hr - 1;
                }
                else
                {
                    minute = minute - 1;
                }
            }
            label1.Text = "0" + hr + ":" + minute + ":" + second;
            second = second - 1;
        }

        private void Form1_Load(object sender, EventArgs e)
        {            

        }

        private void btnPause_Click(object sender, EventArgs e)
        {
            if (btnPause.Text == "Pause")
            {
                timer1.Stop();
                btnPause.Text = "Resume";
            }
            else if (btnPause.Text == "Resume")
            {
                timer1.Start();
                btnPause.Text = "Pause";
            }
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            timer1.Stop();
            label1.Text = "03:00:00";
            btnStart.Enabled = true;
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            initialize();
            timer1.Start();
            btnStop.Enabled = true;
            btnPause.Enabled = true;
            btnStart.Enabled = false;
        }

    }
Sourabh Sharma (150 Posts)