How To Create a Stopwatch in Visual Basic
Submitted by Yorkiebar on Saturday, September 21, 2013 - 04:38.
Language
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...
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:
Button2 - Lap:
Button3 - Stop:
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...
Step 5:
Finally add the function below to update our label on screen for the current time...
Project Complete!
That's it! Below is the full source and project download, thanks for reading!
- Dim running As Boolean = False
- Dim seconds As Integer = 0, minutes As Integer = 0, hours As Integer = 0, lapCount As Integer = 0
- Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
- running = True
- Timer1.Enabled = True
- Timer1.Start()
- End Sub
- Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
- lapCount += 1
- ListBox1.Items.Add("Lap " & lapCount & " - " & hours & ":" & minutes & ":" & seconds)
- End Sub
- Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
- running = False
- Timer1.Stop()
- Timer1.Enabled = False
- End Sub
- Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
- seconds += 1
- If (seconds = 60) Then
- seconds = 0
- minutes += 1
- If (minutes = 60) Then
- minutes = 0
- hours += 1
- End If
- End If
- updateControls()
- End Sub
- Private Function updateControls()
- Label1.Text = hours & ":" & minutes & ":" & seconds
- End Function
- Public Class Form1
- Dim running As Boolean = False
- Dim seconds As Integer = 0, minutes As Integer = 0, hours As Integer = 0, lapCount As Integer = 0
- Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
- running = True
- Timer1.Enabled = True
- Timer1.Start()
- End Sub
- Private Function updateControls()
- Label1.Text = hours & ":" & minutes & ":" & seconds
- End Function
- Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
- lapCount += 1
- ListBox1.Items.Add("Lap " & lapCount & " - " & hours & ":" & minutes & ":" & seconds)
- End Sub
- Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
- running = False
- Timer1.Stop()
- Timer1.Enabled = False
- End Sub
- Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
- seconds += 1
- If (seconds = 60) Then
- seconds = 0
- minutes += 1
- If (minutes = 60) Then
- minutes = 0
- hours += 1
- End If
- End If
- updateControls()
- End Sub
- 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
- 6020 views