How To Make A Stopwatch In VB.NET

Today i am going to teach you how to make a stopwatch in visual basic 2008/ visual basic 2010 the features are start, stop, mark and reset this is not a advanced stopwatch, but this has the basic features of a stopwatch probably this is for beginners so lets get started with opening your visual basic 2008 / visual basic 2010

1 . Create a new project
2 . Add a label, 4 buttons, 1 listbox and a timer
3 . Go to timer properties and change timer interval to 1 
4 . I recommend you to arrange your tools like this

5 . Time for coding double click form and add all these codes
Public Class Form1

    'Author : Mohamed Shimran
    'Blog : http://www.ultimateprogrammingtutorials.blogspot.com

    Dim StopWatch As New Diagnostics.Stopwatch
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim elapsed As TimeSpan = Me.StopWatch.Elapsed
        Label1.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 Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Timer1.Start()
        Me.StopWatch.Start()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Timer1.Stop()
        Me.StopWatch.Stop()
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Me.StopWatch.Reset()
        Label1.Text = "00:00:00:000"
        ListBox1.Items.Clear()
    End Sub

    Private Sub Button3_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
      ListBox1.Items.Add(Label1.Text)
    End Sub
End Class  
6 . Now debug the program and the program would work like this


Hope you enjoyed the tutorial.

6 comments

Your method of describing everything in this article is
really pleasant, every one be capable of without
difficulty be aware of it, Thanks a lot.
Have a look at my website ; online Stopwatch

Reply

Well the stopwatch is great and all, but I don't feel like I really did anything... you see, I'm kinda new to VB and I was wondering if you could explain how the Stopwatch label worked. Thank you for reading! :)

Reply

It's all about timers ! as you can see we enable elapsed !
"A timer which counts upwards from zero for measuring elapsed time is often called a stopwatch" that's the basic idea of a stopwatch. Thanks

Reply

not going well its telling me dat label1 is not declared

Reply

Finally I found the tutorial I need. Thanks :-D
I'd like to ask you what part of your code made all numbers have a zero before units. I'm really looking forward to understand that part.

In my program I don't know how to insert the right code to make it work like yours:

Private Sub Timer1_Tick_1(sender As Object, e As EventArgs) Handles Timer1.Tick
Tempo.secondLabel.Text += 1
If Tempo.secondLabel.Text = "60" Then
Tempo.minuteLabel.Text += 1
Tempo.secondLabel.Text = "00"
End If
End Sub

Thanks

Reply

Post a Comment

Note: Only a member of this blog may post a comment.