Digital Stopwatch using Visual Basic.Net

In this tutorial I'm going to teach you on how to create a simple digital stopwatch that will able you to start the timer when the “Start” button is click, then pause the timer when the “Pause” button is clicked. And you can also clicke the “Continue” button if you want to continue running the timer. And this program has the feature that able’s you to restart the timer anytime you want as long as you click the “Reset” button and finally you can stop the timer if you wish to stop it and the timer will go back to its original value. Below is the sample output in this code. To start building our application open visual basic in your computer and create a new project and name it as "Digital Stopwatch". And then add five (5) buttons in your form: one textbox, one label and one timer. After this design your user interface that looks as shown above. And after designing the user interface, let's proceed in adding a functionality to our application. First double click the form to show the code view and below Public class add this declaration of variables.
  1.     Dim a As String
  2.     Dim b As String
  3.     Dim c As String
  4.     Dim d As String
  5.     Dim y As String
  6.     Dim z As String
  7.     Dim h As String
  8.     Dim m As String
  9.     Dim s As String
  10.     Dim u As String
  11.     Dim v As String
  12.  
Then we will create a sub procedure for running the timer and we will name this sub as “runningtime” and here’s it looks like.
  1.   Sub runningtime()
  2.         If Val(v) < 6 Then
  3.             v = v + 1
  4.             txttime.Text = a + b + ":" + c + d + ":" + y + z + "." + u + v
  5.             'check if u is less then 9
  6.         ElseIf Val(u) < 9 Then
  7.             v = 0
  8.             'incretment u by 1
  9.             u = u + 1
  10.             'display for miliseconds
  11.             txttime.Text = a + b + ":" + c + d + ":" + y + z + "." + u + v
  12.             'check if z is still less than 9
  13.         ElseIf Val(z) < 9 Then
  14.             'set the milisecond to 0
  15.             v = 0
  16.             u = 0
  17.             'increment
  18.             z = z + 1
  19.             'display for second(s)
  20.             txttime.Text = a + b + ":" + c + d + ":" + y + z + "." + u + v
  21.         ElseIf Val(y) < 5 Then
  22.             v = 0
  23.             u = 0
  24.             z = 0
  25.             y = y + 1
  26.             txttime.Text = a + b + ":" + c + d + ":" + y + z + "." + u + v
  27.         ElseIf Val(d) < 9 Then
  28.             v = 0
  29.             u = 0
  30.             z = 0
  31.             y = 0
  32.             d = d + 1
  33.             txttime.Text = a + b + ":" + c + d + ":" + y + z + "." + u + v
  34.         ElseIf Val(c) < 5 Then
  35.             v = 0
  36.             u = 0
  37.             z = 0
  38.             y = 0
  39.             d = 0
  40.             c = c + 1
  41.             txttime.Text = a + b + ":" + c + d + ":" + y + z + "." + u + v
  42.         ElseIf Val(b) < 9 Then
  43.             v = 0
  44.             u = 0
  45.             z = 0
  46.             y = 0
  47.             d = 0
  48.             c = 0
  49.             b = b + 1
  50.             txttime.Text = a + b + ":" + c + d + ":" + y + z + "." + u + v
  51.         ElseIf Val(b) < 9 Then
  52.             v = 0
  53.             u = 0
  54.             z = 0
  55.             y = 0
  56.             d = 0
  57.             c = 0
  58.             b = b + 1
  59.             txttime.Text = a + b + ":" + c + d + ":" + y + z + "." + u + v
  60.         ElseIf Val(a) < 9 Then
  61.             v = 0
  62.             u = 0
  63.             z = 0
  64.             y = 0
  65.             d = 0
  66.             c = 0
  67.             b = 0
  68.             a = a + 1
  69.             'this is the final output if reach the hour value
  70.             txttime.Text = a + b + ":" + c + d + ":" + y + z + "." + u + v
  71.  
  72.             '1 sec = 1000ms
  73.             '1 min = 60000ms
  74.             '1 hr = 36000000ms
  75.  
  76.  
  77.         End If
  78.  
  79.     End Sub
After this, we need also to create another two sub procedure that is responsible for stopping and restarting the timer and name this as “stop time” and “restart”. Here's the code for these two procedures.
  1.  Sub restart()
  2.  
  3.         a = "0"
  4.         b = "0"
  5.         c = "0"
  6.         d = "0"
  7.         y = "0"
  8.         z = "0"
  9.         u = "0"
  10.         v = "0"
  11.  
  12.         h = a + b
  13.         m = c + d
  14.         s = y + z
  15.  
  16.  
  17.     End Sub
  18.     Sub stoptime()
  19.         Timer1.Enabled = False
  20.         a = "0"
  21.         b = "0"
  22.         c = "0"
  23.         d = "0"
  24.         y = "0"
  25.         z = "0"
  26.         u = "0"
  27.         v = "0"
  28.  
  29.         h = a + b
  30.         m = c + d
  31.         s = y + z
  32.         'display as "00:00:00.00"
  33.         txttime.Text = h + ":" + m + ":" + s + "." + u + v
  34.  
  35.     End Sub
Since we have already set our sub procedures lets now add code to our timer. To do this double click our timer and add this single code.
  1.   Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
  2.         'this single code, it simply call the runningtime() sub procedure
  3.         runningtime()
  4.     End Sub
Next we need to add this code to our form load and these how it looks like.
  1.  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  2.         'it do the initialization
  3.         a = "0"
  4.         b = "0"
  5.         c = "0"
  6.         d = "0"
  7.         y = "0"
  8.         z = "0"
  9.         u = "0"
  10.         v = "0"
  11.  
  12.         h = a + b
  13.         m = c + d
  14.         s = y + z
  15.         'display as "00:00:00.00"
  16.         txttime.Text = h + ":" + m + ":" + s + "." + u + v
  17.     End Sub
Next we will add code also for “Start” button and here’s the code.
  1.  Private Sub BtnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnStart.Click
  2.         'start the timer
  3.         Timer1.Start()
  4.         'check if the text of these button is set to continue set by pause button then it set to start
  5.         If BtnStart.Text = "Continue" Then
  6.             BtnStart.Text = "Start"
  7.         End If
  8.     End Sub
For our “Stop” button.
  1. stoptime()
And for “Reset” button.
  1. restart()
And then for our “Pause” button.
  1. Timer1.Enabled = False
  2. BtnStart.Text = "Continue"
And finally our “Exit” button.
  1.     Me.Close()
Then after completing all the steps you can now start to test your program. That’s it for now fellas see you on my next tutorial focusing on manipulating the time using “Timer control”. Thanks for reading.

Add new comment