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
 
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.
VB.NET:
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
 
Yes you can format the timespan returned by stopwatch.elapsed.

VB.NET:
        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)
 
To do that you will use the TotalMinutes property of the TimeSpan. Format the string as Tom showed you.
 
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
 
Back
Top