Keeping Track of Time?

Can anyone help me out with this I am in the process of jumping from VB6 to VB.Net 2005 B2.

I can't seem to be able to get this coded properly. I want to have a simple label that will increment time from the moment I press a "Start" button until the moment I press a "Stop" button. The time would be in the following format hh:mm:ss


Answer this question

Keeping Track of Time?

  • Bullfrog

    Anders, thanks.  I will try this when I get home from work tonight and let you know how it worked for me.

  • jetsetwilly

    Hmmm... Pausing... Well, I think I would use three extra variables for that:



    Private pausing As Boolean
    Private totalPauseSpan As TimeSpan
    Private pauseBegun As DateTime

     


    Then, when the Pause button is clicked, if pausing is False, I set pauseBegun to "Now", pausing to True, and Timer1.Enabled to False.

    Then the Pause button is clicked and pausing is True, I check how long the pause has been, add that TimeSpan to the totalPauseSpan variable, set pausing to False and start the Timer again.

    For the totalPauseSpan to have an impact on your Label, I just edit the "Dim span ..." line to this:


    Dim span As TimeSpan = (DateTime.Now - start) - totalPauseSpan
     


    Complete code (now with milliseconds for more bells and whistles - set the Timer1.Interval to a small value...):



    Public Class Form1
    Private start As DateTime
    Private pausing As Boolean
    Private totalPauseSpan As TimeSpan
    Private pauseBegun As DateTime

    Private Sub StartButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StartButton.Click
    start = DateTime.Now
    Timer1.Enabled = True
    UpdateTimeSpanLabel()
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    UpdateTimeSpanLabel()
    End Sub

    Private Sub UpdateTimeSpanLabel()
    Dim span As TimeSpan = (DateTime.Now - start) - totalPauseSpan
    Label1.Text = span.Hours.ToString("00") & ":" & span.Minutes.ToString("00") & ":" & span.Seconds.ToString("00") & "." & span.Milliseconds.ToString("000")
    End Sub

    Private Sub StopButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StopButton.Click
    Timer1.Enabled = False
    End Sub

    Private Sub PauseButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PauseButton.Click
    If (pausing) Then
    totalPauseSpan = totalPauseSpan + (DateTime.Now - pauseBegun)
    Timer1.Enabled = True
    pausing = False
    Else
    Timer1.Enabled = False
    pauseBegun = DateTime.Now
    pausing = True
    End If
    End Sub
    End Class

     




  • Stefan de Vogelaere

    I'm not completely sure of what your intent is here. The totalPauseSpan variable keeps track of the total paused time span - i.e. the part of the measured time that should not be included.

    However, if you want to start the timer at a certain value when clicking "Start", you could set the totalPausSpan variable to a negative value to start with. Add the following line to your StartButton_Click event and see what happens:


    totalPauseSpan = New TimeSpan(-3, -10, -15)


     



  • dana_lotus

    Ok, that worked perfectly!  I can't, however, figure out how to "Pause" this.  If I want to press Stop, then Start... it restarts it.  I am going to play around with it, but if someone can help me out that would be great.

  • DanSequel

    Try adding a Timer component to your form. Set the Interval property to whatever time granularity you want (one second, maybe).

    Add a DateTime object to your form.

    Then add code to the Button's Click event and the Timer's Tick event:


    Public Class Form1

        Private start As DateTime

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            start = DateTime.Now
            Timer1.Enabled = True
            UpdateTimeSpanLabel()
        End Sub

        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            UpdateTimeSpanLabel()
        End Sub

        Private Sub UpdateTimeSpanLabel()
            Dim span As TimeSpan = DateTime.Now - start
            Label1.Text = span.Hours.ToString("00") & ":" & span.Minutes.ToString("00") & ":" & span.Seconds.ToString("00")
        End Sub
    End Class



  • Yiannis Piros

    Thank you very much!

    One last quick question on this code...

    How can I set the "pauseSpan" to be something like, 3 hours, 21 minutes and 32 seconds or something... have it start at a certain time

    Thank you VERY much in advance.

  • Keeping Track of Time?