How To Create a Stopwatch in Visual Basic

Submitted by: 
Language: 
Visitors have accessed this post 21728 times.

Introduction:
Welcome to my tutorial on how to create a stopwatch in Visual Basic with lapping system.

Steps of Creation:
Step 1:

So the way this is going to work is we will haev a timer running at a one second interval and each time it runs it will add one to our second count. It will then check our second count to see if it has been one minute and if it has it will add one to our minute count and reset seconds back to 0. It will do the same for minutes to hours. So first lets make our form, we will need...
- Label1 - Contains current time in the format (hours:minutes:seconds)
- Button1 - Start the timer
- Button2 - Lap the time
- Button3 - Stop the timer
- Listbox1 - Contains lapped times
- Timer1 - Counts the time

Step 2:
Now create our hours, minutes and seconds variables...

  1. Dim running As Boolean = False
  2. Dim seconds As Integer = 0, minutes As Integer = 0, hours As Integer = 0, lapCount As Integer = 0

We also created a running variable just to see when it is running, I haven't used it to output the status but you can easily add that to a new label.

Step 3:
Now enter the following for the button codes:

Button1 - Start:

  1. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  2. running = True
  3. Timer1.Enabled = True
  4. Timer1.Start()
  5. End Sub

Button2 - Lap:

  1. Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
  2. lapCount += 1
  3. ListBox1.Items.Add("Lap " & lapCount & " - " & hours & ":" & minutes & ":" & seconds)
  4. End Sub

Button3 - Stop:

  1. Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
  2. running = False
  3. Timer1.Stop()
  4. Timer1.Enabled = False
  5. End Sub

Step 4:
Next make sure your timer1 interval is set to 1000 and it is not enabled by default, then add this code to increment our current time count...

  1. Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
  2. seconds += 1
  3. If (seconds = 60) Then
  4. seconds = 0
  5. minutes += 1
  6. If (minutes = 60) Then
  7. minutes = 0
  8. hours += 1
  9. End If
  10. End If
  11. updateControls()
  12. End Sub

Step 5:
Finally add the function below to update our label on screen for the current time...

  1. Private Function updateControls()
  2. Label1.Text = hours & ":" & minutes & ":" & seconds
  3. End Function

Project Complete!
That's it! Below is the full source and project download, thanks for reading!

  1. Public Class Form1
  2. Dim running As Boolean = False
  3. Dim seconds As Integer = 0, minutes As Integer = 0, hours As Integer = 0, lapCount As Integer = 0
  4. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  5. running = True
  6. Timer1.Enabled = True
  7. Timer1.Start()
  8. End Sub
  9.  
  10. Private Function updateControls()
  11. Label1.Text = hours & ":" & minutes & ":" & seconds
  12. End Function
  13.  
  14. Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
  15. lapCount += 1
  16. ListBox1.Items.Add("Lap " & lapCount & " - " & hours & ":" & minutes & ":" & seconds)
  17. End Sub
  18.  
  19. Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
  20. running = False
  21. Timer1.Stop()
  22. Timer1.Enabled = False
  23. End Sub
  24.  
  25. Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
  26. seconds += 1
  27. If (seconds = 60) Then
  28. seconds = 0
  29. minutes += 1
  30. If (minutes = 60) Then
  31. minutes = 0
  32. hours += 1
  33. End If
  34. End If
  35. updateControls()
  36. End Sub
  37. End Class

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

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.