Write a Stopwatch Program using vb.net

Web Design and Digital Marketing

Write a Stopwatch Program using vb.net

Write a Stopwatch Program using vb.net. Your program should have a label to display the time as it ticks in seconds, and three buttons start, stop, and reset. Add code to the buttons. Set the timer interval to 1000 which is equal to one second.

NOTE: To solve this problem we will be using VB.NET as our programming language. You will also need visual studio IDE to follow along.

SOLUTION

1. Design the GUI

Design your UI elements as in the picture or as you wish. Make sure you give your buttons and labels are properly named. I have named mine as in the code below.

giving your objects proper names is a good practice when programming.

stopwatch UI design

2. Write Code

Before you start writing code for this question, I identify all that is needed to come up with a solution. In this case, we need to code all our buttons.

  1. Start Button – to start our timber
  2. Mark Button – to mark a single interval
  3. Stop Button – to stop the timer from running maintaining the current timestamp
  4. Reset Button – to reset the timer to 0
  5. Exit Button – to close/exit our application
Dim StopWatch As New Diagnostics.Stopwatch
Private Sub mainTimer_Tick(sender As Object, e As EventArgs) Handles mainTimer.Tick
Dim elapsed As TimeSpan = Me.StopWatch.Elapsed
timeLabel.Text = String.Format("{0:00}:{1:00}:{2:00}:{3:00}", Math.Floor(elapsed.TotalHours), elapsed.Minutes, elapsed.Seconds, elapsed.Milliseconds)
End Sub

Private Sub exitButton_Click(sender As Object, e As EventArgs) Handles exitButton.Click
Close()
End Sub

Private Sub StartButton_Click(sender As Object, e As EventArgs) Handles StartButton.Click
mainTimer.Start()
Me.StopWatch.Start()
End Sub

Private Sub stopButton_Click(sender As Object, e As EventArgs) Handles stopButton.Click
mainTimer.Stop()
Me.StopWatch.Stop()
End Sub

Private Sub ResetButton_Click(sender As Object, e As EventArgs) Handles ResetButton.Click
Me.StopWatch.Reset()
timeLabel.Text = "00:00:00:000"
timeListBox.Items.Clear()
End Sub

Private Sub markButton_Click(sender As Object, e As EventArgs) Handles markButton.Click
timeListBox.Items.Add(timeLabel.Text)
End Sub

Above i have used one of many solutions to solve our problem, as you can see I have used some comments in my code. It is optional to use comments in your code, but it is a best practice to use comments for it will save you time in the future wen you debug you code.

3. Run and test

Now that our app UI is designed and our code is written it is time for us to run and test our app to see if we did make any mistakes.

stopwatch testRun

Above i have used one of many solutions to solve our problem, as you can see I have used some comments in my code. It is optional to use comments in your code, it is a best practise to use comments for it will save you time in the future wen you debug you code.

Conclusion

Now that we have answered our question correctly come back again for the next exam practice question. Like share and subscribe to our YouTube channel.

Now that we have answered our question correctly come back again for the next exam practice question. Like share and subscribe to our youtube channel.

Leave a Comment

Your email address will not be published. Required fields are marked *