Pause then change stopwatch time.

Tezzies

Member
Joined
Mar 4, 2010
Messages
7
Programming Experience
Beginner
Hi again,

I have a StopWatch running, I convert the milliseconds to the following format.
00:00:00 i.e HH:MM:SS which is a label.

I can pause the the stopwatch and resume but I dont seem to be able to edit the stopwatchs time. So lets say my time says 00:00:02 , i now want to pause the program which I can, but also change the stopwatch time to 00:00:10 and resume counting.

Is this possible or am I asking for the moon on a stick.

Many thanks in advance
 
First up, why would you convert the Milliseconds to that format when the TimeSpan returned by the Stopwatch.Elapsed property already has Hours, Minutes and Seconds properties.

As for the question, the Stopwatch is for measuring elapsed time, so you're not supposed to change its value. If you want a different value then don't use the raw Elapsed value. Create a separate TimeSpan that contains your desired offset and then simply add it to the Elapsed property value and use the result. For instance, if you create a TimeSpan that represents 2 seconds:
VB.NET:
Private offset As TimeSpan = TimeSpan.FromSeconds(2)
and then you add that to your elapsed time:
VB.NET:
Dim elapsed As TimeSpan = myStopWatch.Elapsed + Me.offset
the result will always be 2 seconds greater than what your Stopwatch has measured. You can use whatever offset you like and you can add or subtract.
 
Thanks for the reply,

I 've been working on this and I am pretty sure I am doing what you advised on the previous post. I convert my new time to milliseconds then try send this across to my offset variable which is lMil . I stop then restart my stopwatch and with the offset added but unfortunately the stopwatch starts from zero again.

Heres my code


PHP:
    Public Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        Dim offset As TimeSpan = TimeSpan.FromMilliseconds(lMil) ' lMil is set by the new time entered when we adjust the stopwatch
        Dim elapsed As TimeSpan = myFunctions.stpWatchInfo.Elapsed + offset 'holds the stopwatch time elapsed info ***tHIS IS THE LINE THAT GETS IGNORED***
        Dim sColour As String
        Dim sLength As Integer
        Dim sColourL As Integer

        'Basically every tick of the clock (half a second) the following code executes
        LblTime.Text = String.Format("{0:00}:{1:00}:{2:00}", _
                                   Math.Floor(elapsed.TotalHours), _
                                   elapsed.Minutes, _
                                   elapsed.Seconds)

--------------------------------------------------------------------------
'Here is where I grab covert and send time to lMil


    Private Sub BtnEnterTime_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnEnterTime.Click
        Dim sNewTime As Long
        Dim lSecsInMil As Long
        Dim lMinsInMil As Long
        Dim lHrsInMil As Long


        'Converts my new clock figure into milliseconds
        lSecsInMil = (Tbx5.Text & Tbx6.Text) * 1000
        lMinsInMil = (Tbx3.Text & Tbx4.Text) * 60000
        lHrsInMil = (Tbx1.Text & Tbx2.Text) * 3600000

        sNewTime = lSecsInMil + lMinsInMil + lHrsInMil

        'places the new figure in lMil which is used as my offset
        lMil = sNewTime
        '
        'Reset time on stopwatch then start , the offset should be displayed instead of 00:00:00
        stpWatchInfo.Reset()
        stpWatchInfo.Start()

        Me.Hide()
    End Sub


Many thanks in advance
 
Back
Top