Skip to main content

How To Create a Stopwatch in C#


In this tutorial, I will teach you how to create a program that has a function of a digital stopwatch. Stopwatch is a timepiece that can be started or stopped for exact timing as of a race or any activity.

Now, let's start this tutorial!

1. Let's start with creating a Windows Form Application for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New Project, and choose Windows Application.

2. Next, add four Buttons named Button1 and labeled it as "Start” for starting the time, Button 2 and labeled it as "Stop” for stopping the time, Button3 and labeled it as "Mark” for marking the time, and Button4 and labeled it as "Reset” for resetting the time back to 0. Insert a timer named Timer1 for creating the time, Label named Label1 for displaying the time, and ListView named ListView1 for displaying the marked time. You must design your interface like this:

design

3. First, we will code for Timer1. Put this code below.

  1. System.Diagnostics.Stopwatch StopWatch = new System.Diagnostics.Stopwatch();
  2. public void Timer1_Tick(System.Object sender, System.EventArgs e)
  3. {
  4. TimeSpan elapsed = this.StopWatch.Elapsed;
  5. Label1.Text = string.Format("{0:00}:{1:00}:{2:00}:{3:00}", Math.Floor(elapsed.TotalHours), elapsed.Minutes, elapsed.Seconds, elapsed.Milliseconds);
  6. }

We have first initialized variable StopWatch As New Diagnostics.Stopwatch. Stopwatch Class provides a set of methods and properties that you can use to accurately measure elapsed time.

We have initialized variable elapsed As TimeSpan which will be equal to the elapsed time of variable StopWatch. A TimeSpan object represents a time interval (duration of time or elapsed time) that is measured as a positive or negative number of days, hours, minutes, seconds, and fractions of a second. The TimeSpan structure can also be used to represent the time of day, but only if the time is unrelated to a particular date. We used Label1 as the timer that has the format like the real stopwatch. We have the format "{0:00}:{1:00}:{2:00}:{3:00}" for time. Math.Floor Method here returns the largest integer less than or equal to the specified double-precision floating-point number. It does have elapsed time for hours, minutes, seconds, and milliseconds.

4. Then we will code for Button1 to enable and start the timer and the stopwatch.

  1. public void Button1_Click(System.Object sender, System.EventArgs e)
  2. {
  3. Timer1.Start();
  4. this.StopWatch.Start();
  5. }

5. In Button2 as the stop button, put this code below to stop the time in the stopwatch.

  1. public void Button1_Click(System.Object sender, System.EventArgs e)
  2. {
  3. Timer1.Start();
  4. this.StopWatch.Start();
  5. }

6. In Button3 as marking the time or putting the time into the ListBox, put this code below.

  1. public void Button3_Click_1(System.Object sender, System.EventArgs e)
  2. {
  3. ListBox1.Items.Add(Label1.Text);
  4. }

7. For Button4 as reset button, put this code below to go back the time into 0 or restart it.

  1. public void Button4_Click(System.Object sender, System.EventArgs e)
  2. {
  3. this.StopWatch.Reset();
  4. Label1.Text = "00:00:00:000";
  5. ListBox1.Items.Clear();
  6. }


Full source code:

  1. using System.Diagnostics;
  2. using System;
  3. using System.Windows.Forms;
  4. using System.Collections;
  5. using System.Drawing;
  6. using System.Data;
  7. using System.Collections.Generic;
  8.  
  9.  
  10.  
  11. namespace StopWatch
  12. {
  13. public partial class Form1
  14. {
  15. public Form1()
  16. {
  17. InitializeComponent();
  18.  
  19. }
  20.  
  21.  
  22. System.Diagnostics.Stopwatch StopWatch = new System.Diagnostics.Stopwatch();
  23. public void Timer1_Tick(System.Object sender, System.EventArgs e)
  24. {
  25. TimeSpan elapsed = this.StopWatch.Elapsed;
  26. Label1.Text = string.Format("{0:00}:{1:00}:{2:00}:{3:00}", Math.Floor(elapsed.TotalHours), elapsed.Minutes, elapsed.Seconds, elapsed.Milliseconds);
  27. }
  28. public void Button1_Click(System.Object sender, System.EventArgs e)
  29. {
  30. Timer1.Start();
  31. this.StopWatch.Start();
  32. }
  33.  
  34. public void Button2_Click(System.Object sender, System.EventArgs e)
  35. {
  36. Timer1.Stop();
  37. this.StopWatch.Stop();
  38. }
  39.  
  40. public void Button4_Click(System.Object sender, System.EventArgs e)
  41. {
  42. this.StopWatch.Reset();
  43. Label1.Text = "00:00:00:000";
  44. ListBox1.Items.Clear();
  45. }
  46.  
  47. public void Button3_Click_1(System.Object sender, System.EventArgs e)
  48. {
  49. ListBox1.Items.Add(Label1.Text);
  50. }
  51. }
  52. }


Output:

output

For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below.

Best Regards,

Engr. Lyndon Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer

If you have some queries, feel free to contact the number or e-mail below.
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]

Add and Follow me on Facebook: https://www.facebook.com/donzzsky

Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Visit my website at: www.campcodes.com

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.