How do I use a timer control to show elapsed time in a label.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hatch
    New Member
    • Nov 2009
    • 4

    How do I use a timer control to show elapsed time in a label.

    The project I am working on has to have start and stop buttons on a form and a label to show the eslaped time once the start button is pushed. The elapsed tome should stop when the stop button is pressed. I'm not sure how to display the elapsed time in the label. Can someone help?
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    Use two DateTime objects (variables).
    DateTime Startime
    DateTime Stoptime

    When you press your Start button set Startime to DateTime.Now and start your timer.

    On each .Tick of the timer update your label.
    The elapsed time would be DateTime.Now - StartTime.

    You can then set the Stoptime when you click the Stop button

    Comment

    • hatch
      New Member
      • Nov 2009
      • 4

      #3
      Ok, thanks that worked. I now have to other problems I still need to figure out. When the start button is clicked it starts the elapsed time. When the stop button is pressed the elapsed time should stop, which it does. Once I hit the start button again (after the stop button is pressed) the elapsed time starts over. I need to be able to hit the start button and resume the elapsed time where it left off. Here is some of my code:

      public partial class Form1 : Form
      {
      DateTime startTime = new DateTime();
      TimeSpan elapsedTime = new TimeSpan();
      public Form1()
      {
      InitializeCompo nent();

      }

      private void startBtn_Click( object sender, EventArgs e)
      {
      startBtn.Enable d = false;
      stopBtn.Enabled = true;
      resetBtn.Enable d = true;
      timer1.Interval = (1000) * (1);
      timer1.Enabled = true;
      timer1.Start();
      startTime = DateTime.Now;


      }

      private void stopBtn_Click(o bject sender, EventArgs e)
      {
      stopBtn.Enabled = false;
      startBtn.Enable d = true;
      resetBtn.Enable d = true;
      timer1.Stop();

      }

      private void timer1_Tick(obj ect sender, EventArgs e)
      {

      elapsedTime = DateTime.Now - startTime;
      label1.Text = elapsedTime.ToS tring();
      }
      }

      I'm not sure how to use the elapsed time variable in a if statement to check its value. The elapsed time variable is set up as a timespan. I also would like the elapsed time to display as 00.00.00 and I'm not sure how to go about this.

      Comment

      • hatch
        New Member
        • Nov 2009
        • 4

        #4
        How do I check a value in an If statement for a TimeSpan or DateTime object?

        For the project I'm working on there is a label, start button, stop button, reset button, and a timer control. When the start button is clicked it starts the elapsed time. When the stop button is pressed the elapsed time should stop, which it does. Once I hit the start button again (after the stop button is pressed) the elapsed time starts over. I need to be able to hit the start button and resume the elapsed time where it left off. Here is some of my code:

        Code:
        public partial class Form1 : Form
        {
        DateTime startTime = new DateTime();
        TimeSpan elapsedTime = new TimeSpan();
        public Form1()
        {
        InitializeComponent();
        
        }
        
        private void startBtn_Click(object sender, EventArgs e)
        {
        startBtn.Enabled = false;
        stopBtn.Enabled = true;
        resetBtn.Enabled = true;
        timer1.Interval = (1000) * (1); 
        timer1.Enabled = true; 
        timer1.Start();
        startTime = DateTime.Now;
        
        
        }
        
        private void stopBtn_Click(object sender, EventArgs e)
        {
        stopBtn.Enabled = false;
        startBtn.Enabled = true;
        resetBtn.Enabled = true;
        timer1.Stop();
        
        }
        
        private void timer1_Tick(object sender, EventArgs e)
        {
        
        elapsedTime = DateTime.Now - startTime;
        label1.Text = elapsedTime.ToString();
        }
        }
        I'm not sure how to use the elapsed time variable in a if statement to check its value. The elapsed time variable is set up as a timespan. I also would like the elapsed time to display as 00.00.00 and I'm not sure how to go about this.

        Can some help with some ideas?

        Thanks!
        Last edited by tlhintoq; Dec 10 '09, 11:28 PM. Reason: [CODE] ...Your code goes between code tags [/CODE]

        Comment

        • tlhintoq
          Recognized Expert Specialist
          • Mar 2008
          • 3532

          #5
          Hatch - You already had a thread started for this. In the future please don't start multiple threads for the same question. It divides efforts to help you. I have merged the two threads.

          Comment

          • tlhintoq
            Recognized Expert Specialist
            • Mar 2008
            • 3532

            #6
            TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.

            Comment

            • tlhintoq
              Recognized Expert Specialist
              • Mar 2008
              • 3532

              #7
              SInce you are making a stopwatch then you should probably follow the UI that people already know for a stopwatch.
              Start always starts at zero.
              But if you want to keep timing then add a button marked "Lap" to add the current elapsed time to a list of lap times, but keeps the timer runner.
              I also would like the elapsed time to display as 00.00.00 and I'm not sure how to go about this.
              Take a look at the various overrides for the ToString() method. You will see that one of them allows you to include a format such as:
              Code:
              ToString("HH:mm:ss.fff");

              Comment

              • hatch
                New Member
                • Nov 2009
                • 4

                #8
                Sorry about that. I'm not familiar with the site. Thanks for the advice. Will do next time. Where could i find out more on the UI people already know for the Stopwatch?

                Comment

                Working...