Geeks With Blogs
Rodney Vinyard - .NET & SQL Developer When all is said and done, more will be said than done

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;
        }
    }

Posted on Tuesday, April 7, 2009 2:45 AM Dates | Back to top


Comments on this post: Show elapsed time on a WinfForm in C#

# re: Show elapsed time on a WinfForm in C#
Requesting Gravatar...
Useful little class ... thanks
Left by Andrew O on Apr 30, 2009 2:10 PM

# re: Show elapsed time on a WinfForm in C#
Requesting Gravatar...
very good article for the freshers.
keep posting such articles
thanks
Left by ankit chitkara on Jul 23, 2009 3:28 AM

# re: Show elapsed time on a WinfForm in C#
Requesting Gravatar...
hi ankita i am just looking u on internet.can we be frand?
Left by gaurav on Nov 04, 2009 4:13 PM

# re: Show elapsed time on a WinfForm in C#
Requesting Gravatar...
Hello I am so delighted I found your blog, I really found you by mistake.
Left by authentic retro jordans on Dec 18, 2010 1:49 AM

Your comment:
 (will show your gravatar)


Copyright © Rodney Vinyard | Powered by: GeeksWithBlogs.net