• I removed the www from the URL, please update your bookmarks and pay attention to the bottom of the screen if using Google Chrome to allow push notifications again.

Stopwatch/Timer

Cabose

Active member
Joined
Oct 18, 2006
Messages
39
Programming Experience
Beginner
Hi, I was wanting to make a stopwatch/timer to my program and i was wanting to know how to go about this.

Thanks,
Cabose
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,061
Location
Sydney, Australia
Programming Experience
10+
Use the Stopwatch and Timer classes. You can use a Stopwatch to monitor elapsed time and a Timer to trigger an update of the display of that time, e.g.
Code:
Private myStopwatch As New Stopwatch

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    Dim elapsed As TimeSpan = Me.myStopwatch.Elapsed

    Me.Label1.Text = String.Format("{0:00}:{1:00}:{2:00}.{3:000}", _
                                   Math.Floor(elapsed.TotalHours), _
                                   elapsed.Minutes, _
                                   elapsed.Seconds, _
                                   elapsed.Milliseconds)
End Sub
 

sisquo76

Active member
Joined
Dec 1, 2008
Messages
28
Location
Bosnia and Herzegovina
Programming Experience
3-5
I have one question regarding stopwatch. Is it possible to display minutes:seconds, but minutes more than 60 (like 90 minutes in soccer or so)?

Thanks,

sisquo76
 

Tom

Well-known member
Joined
Aug 23, 2005
Messages
746
Programming Experience
5-10
Yes you can format the timespan returned by stopwatch.elapsed.

Code:
        Dim ts As TimeSpan = swTimer.Elapsed
        Dim strResult As String = ""

        strResult = String.Format("{0:#0}:{01:00}:{02:00} ", ts.Hours, ts.Minutes, ts.Seconds)
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,277
Location
Norway
Programming Experience
10+
To do that you will use the TotalMinutes property of the TimeSpan. Format the string as Tom showed you.
 

Tom

Well-known member
Joined
Aug 23, 2005
Messages
746
Programming Experience
5-10
Opps sorry about that... I went and posted almost the exact same thing as JM. And yes JohnH is correct, use TotalXXX to the total by the time specified
 
Top Bottom