Results 1 to 7 of 7

Thread: displaying elapsed time?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    200

    displaying elapsed time?

    I am wondering if there is a simple way to displaying time elapsed as is it is happening. Instead of getting the date time at start and subtracting from datetime at end. I am looking for a 00:00:00 format as in hrs:min:sec. I thought about maybe adding a timer and somehow converting the milliseconds into the others. Or same with using the stopwatch class.

    My main issue is displaying it in the 00:00:00 format as it happens.

  2. #2
    Frenzied Member Lightning's Avatar
    Join Date
    Oct 2002
    Location
    Eygelshoven
    Posts
    1,611

    Re: displaying elapsed time?

    You should set a start datetime, the use a timer to fire every 100ms (or so). In the tick-event you substract the startdate time from the current date time and you update the label
    VB6 & C# (WCF LINQ) mostly


    If you need help with a WPF/WCF question post in the NEW WPF & WCF forum and we will try help the best we can

    My site

    My blog, couding troubles and solutions

    Free online tools

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    200

    Re: displaying elapsed time?

    I was thinking of that approach already, I just wanted to see if there was a better way of going about it first.

  4. #4
    Junior Member
    Join Date
    Aug 2012
    Posts
    25

    Re: displaying elapsed time?

    The way I would do it is use 5 labels, 00:00:00, the first 00 would be named Hours, the second 00 would be named Minutes, and the third 00 would be named Seconds. On the form loadup make the timer start, this timer should have an interval of 1 second or in other words just make it 1,000. Then in the timer's tick sub you would do something along the lines of this;

    Code:
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            If Seconds.Text <= 9 Then
                Seconds.Text = 0 & Seconds.Text + 1
            ElseIf Seconds.Text >= 10 Then
                Seconds.Text = Seconds.Text + 1
            End If
            If Seconds.Text = 60 Then
                If Minutes.Text <= 9 Then
                    Minutes.Text = 0 & Minutes.Text + 1
                ElseIf Minutes.Text >= 10 Then
                    Minutes.Text = Minutes.Text + 1
                End If
                Seconds.Text = 0
            ElseIf Minutes.Text = 60 Then
                If Hours.Text <= 9 Then
                    Hours.Text = 0 & Hours.Text + 1
                ElseIf Hours.Text >= 10 Then
                    Hours.Text = Hours.Text + 1
                End If
                Minutes.Text = 0
            End If
        End Sub
    Last edited by Providence; Aug 26th, 2012 at 04:24 PM. Reason: Didn't read question completely

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    200

    Re: displaying elapsed time?

    Ok maybe I will try that.

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: displaying elapsed time?

    i'd use a stopwatch:


    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Dim sw As New Stopwatch
    4.     Private WithEvents tmr As New Timer
    5.  
    6.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    7.         sw.Start()
    8.         tmr.Interval = 1000 '1 second
    9.         tmr.Start()
    10.     End Sub
    11.  
    12.     Private Sub tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmr.Tick
    13.         Label1.Text = String.Format("{0:00}:{1:00}:{2:00}", sw.Elapsed.Hours, sw.Elapsed.Minutes, sw.Elapsed.Seconds)
    14.     End Sub
    15.  
    16. End Class
    Last edited by .paul.; Aug 27th, 2012 at 06:20 AM. Reason: error

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    200

    Re: displaying elapsed time?

    Thanks paul. I tried both, the stopwatch approach is a little easier on performance, which is needed in this application Thank you both.

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