Creating a StopWatch in VB.NET

Stopwatch is a timepiece that can be started or stopped for exact timing as of a race or any activity. In this article, we will going to create a program that has the capabilities and control like a stopwatch. 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. Now put this code for your code module.
  1. Public Class Form1
  2.  
  3.     Dim StopWatch As New Diagnostics.Stopwatch
  4.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
  5.         Dim elapsed As TimeSpan = Me.StopWatch.Elapsed
  6.         Label1.Text = String.Format("{0:00}:{1:00}:{2:00}:{3:00}", Math.Floor(elapsed.TotalHours), elapsed.Minutes, elapsed.Seconds, elapsed.Milliseconds)
  7.     End Sub
  8.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  9.         Timer1.Start()
  10.         Me.StopWatch.Start()
  11.     End Sub
  12.  
  13.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  14.         Timer1.Stop()
  15.         Me.StopWatch.Stop()
  16.     End Sub
  17.  
  18.     Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
  19.         Me.StopWatch.Reset()
  20.         Label1.Text = "00:00:00:000"
  21.         ListBox1.Items.Clear()
  22.     End Sub
  23.  
  24.     Private Sub Button3_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
  25.         ListBox1.Items.Add(Label1.Text)
  26.     End Sub
  27. End Class

Explanation:

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. Our Button1 is for starting the stopwatch to tick its 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. Our Button2 is used for stopping the time. We stop the timer and the stopwatch variable. Button3 is used for marking the time, meaning saving it to the ListBox. Lastly, Button4 is used for resetting the time back to 0.

Output:

output output Download the source code below and try it! :) 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 R. Bermoy
IT Instructor/System Developer/Android Developer
Mobile: 09079373999
Telephone: 826-9296
E-mail:[email protected]

Visit and like my page on Facebook at: Bermz ISware Solutions

Subscribe at my YouTube Channel at: SerBermz

Add new comment