Results 1 to 6 of 6

Thread: [RESOLVED] Stopwatch Help

  1. #1

    Thread Starter
    Addicted Member mouse88's Avatar
    Join Date
    Mar 2009
    Location
    South Wales, United Kingdom
    Posts
    225

    Resolved [RESOLVED] Stopwatch Help

    I am using a stop watch to get the elapsed minutes and seconds which will then be displayed to user. How to I go about getting the elapsed time in the following format:

    00:00

  2. #2
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    523

    Re: Stopwatch Help

    By stopwatch I assume you mean the timer Object. That is not designed to give you the time between two events. It is designed to trigger after a certain amount of time - say you want a 10 second pause.

    What I would do is use the Now() function to get the current time at the start of the process and end of the process. Then used the DateDiff function to determine the time difference between the two times in Seconds. Once you have the number of seconds, you do a little math to figure out how many minutes and seconds, then format that, just building it up. You will have to format you minutes and seconds to ensure they give you two numbers. You can use a Format string for that.

    I hope that helps.

  3. #3

    Thread Starter
    Addicted Member mouse88's Avatar
    Join Date
    Mar 2009
    Location
    South Wales, United Kingdom
    Posts
    225

    Re: Stopwatch Help

    Ok cool i'm actually using the stopwatch class not a timer. I can get the time but am not sure about how to format the time.

  4. #4
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Stopwatch Help

    Hey,

    Have you looked at the documentation for the StopWatch Class?

    http://msdn.microsoft.com/en-us/libr...stopwatch.aspx

    It would appear to tell you how to do exactly what you want.

    Gary

  5. #5
    Hyperactive Member
    Join Date
    Dec 2006
    Posts
    293

    Re: Stopwatch Help

    Heres is some code for what i used. The format i used was Hour:Minute:Seconds:Millisecond

    Code:
     Private watch As Diagnostics.Stopwatch
    Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
            Timer1.Enabled = True
            Me.watch = Diagnostics.Stopwatch.StartNew
            Me.Timer1.Start()
        End Sub
    
        Private Sub BtnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
            Timer1.Stop()
                  Timer1.Dispose()
            Timer1.Enabled = False
            lblTime.Text = ("00:00")
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            Dim elapsed As TimeSpan = Me.watch.Elapsed
            Me.lblTime.Text = String.Format("{0:00}:{1:00}", _
                                           elapsed.Seconds, _
                                           elapsed.Milliseconds)
        End Sub
    Ive Changed it for your 00:00

  6. #6

    Thread Starter
    Addicted Member mouse88's Avatar
    Join Date
    Mar 2009
    Location
    South Wales, United Kingdom
    Posts
    225

    Re: Stopwatch Help

    Ok thanks guys problem solved

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width