Mission:  Show elapsed time on a WinfForm in C#

thanks to Mahesh Chand

original article:  http://www.c-sharpcorner.com/UploadFile/mahesh/WorkingwithTimerControlinCSharp11302005054911AM/WorkingwithTimerControlinCSharp.aspx

1) add stop & start buttons and a timer control on a web form

2) put code behind the buttons (below), that consumes the StopWatch class (below)

    public partial class Form1 : Form
    {
        static void Main()
        {
            Application.Run(new Form1());
        }
        public Form1()
        {
            InitializeComponent();
        }

        StopWatch stopWatch = new StopWatch();

        private void btnStart_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            stopWatch.Start();
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            stopWatch.Stop();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            lblElapsedTime.Text = stopWatch.GetElapsedTimeString();
        }
    }

    public class StopWatch
    {

        private DateTime startTime;
        private DateTime stopTime;
        private DateTime currentTime;
        private bool running = false;


        public void Start()
        {
            this.startTime = DateTime.Now;
            this.running = true;
        }


        public void Stop()
        {
            this.stopTime = DateTime.Now;
            this.running = false;
        }


        // elaspsed time in milliseconds
        public string GetElapsedTimeString()
        {
            TimeSpan interval;

            if (!running)
                return ""; //interval = DateTime.Now - startTime;
            else
            {
                this.currentTime = DateTime.Now;
                interval = currentTime - startTime;
            }

            int days = interval.Days;
            double hours = interval.Hours;
            double mins = interval.Minutes;
            double secs = interval.Seconds;
            string x = "";
            if (days != 0)
            {
                x += days.ToString() + ":";
            }
            if (hours != 0)
            {
                x += hours.ToString("00") + ":";
            }
            x += mins.ToString("00") + ":";
            x += secs.ToString("00");

            return x;
        }
        public double GetElapsedMilliseconds()
        {
            TimeSpan interval;

            if (running)
                interval = DateTime.Now - startTime;
            else
                interval = stopTime - startTime;

            return interval.TotalMilliseconds;
        }


        // elaspsed time in seconds
        public double GetElapsedTimeSecs()
        {
            TimeSpan interval;

            if (running)
                interval = DateTime.Now - startTime;
            else
                interval = stopTime - startTime;

            return interval.TotalSeconds;
        }
    }