VB.net real-time time elapsed feature

31,602

Solution 1

I suggest you use only 1 timer:

Public Class Form2

Private _elapseTimerRunning As Boolean = False
Private _elapseStartTime As DateTime

Private Sub Form2_Load(sender As Object, e As EventArgs) Handles Me.Load
    Timer1.Interval = 1000
    Timer1.Enabled = True
End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    txtTime.Text = Now.ToString("h:mm:ss tt")
    If _elapseTimerRunning = True Then
        Dim elapsedtime = DateTime.Now.Subtract(_elapseStartTime)
        txtElapsed.Text = String.Format("{0}hr : {1}min : {2}sec", elapsedtime.Hours, elapsedtime.Minutes, elapsedtime.Seconds)
    End If
End Sub

Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
    _elapseStartTime = DateTime.Now
    _elapseTimerRunning = True
End Sub

Private Sub btnStop_Click(sender As Object, e As EventArgs) Handles btnStop.Click
    _elapseTimerRunning = False
End Sub

End Class

Solution 2

An example for displaying the elapsed time the application has run.

Public Class Form1
    'shows elapsed time that the app has run
    '
    Dim elapTimer As New Threading.Timer(AddressOf tick, Nothing, 1000, 1000)
    Dim stpw As Stopwatch = Stopwatch.StartNew

    Private Sub tick(state As Object)
        If stpw.IsRunning Then
            'format - http://msdn.microsoft.com/en-us/library/ee372287.aspx
            Me.Invoke(Sub()
                          Label1.Text = stpw.Elapsed.ToString("d\ \ hh\:mm\:ss\.ff")
                      End Sub)
        End If
    End Sub

End Class

To add start/stop functionality using buttons:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    'start the elapsed timer
    stpw.Start() 'continue 
    'or
    'stpw.Restart() 'restart
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    stpw.Stop()
End Sub

Solution 3

Something like this would help you: (untested, but will give you a starter)

Dim hr, min, sec As Integer 'msec;

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Timer1.Enabled = True
    Timer2.Enabled = True
End Sub

Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    'msec++;
    'if(msec == 60)  { msec = 0; sec++; }

    sec+=1;
    if(sec  = 60) Then 
      sec = 0 : min+=1
    end if
    if(min  = 60) Then
      min = 0 : hr+=1
    end if
    if(hr = 24)  Then
      hr  = 0 : min = 0 : sec = 0
    end if   

    'TimeElapsed.Text = String.Format("{0}:{1}:{2} {3}", hr, min, sec, msec)
     TimeElapsed.Text = String.Format("{0}:{1}:{2}", hr, min, sec) 
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    Time.Text = Date.Now.ToString("h:mm:ss tt")
End Sub

NOTE: Timer2 will run for every second.

Share:
31,602
Marc Intes
Author by

Marc Intes

currently training about the basics in VB.net

Updated on September 18, 2020

Comments

  • Marc Intes
    Marc Intes over 3 years

    I already have created a real time clock that synchronizes with the computer time and is being displayed in a label.

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Timer1.Enabled = True
    End Sub
    
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Time.Text = Date.Now.ToString("h:mm:ss tt")
    End Sub
    

    I want to make a real-time time elapsed feature that keeps on counting the seconds/minutes/hours elapsed from the time it started till the time it stops and it would be basing on the real-time clock i have created. I would be creating a start and stop button for this. Is this possible? Thanks in advance.


    I am now able to complete everything and i added a feature that records the starting and ending time based on my real time clock. Here is my working code:

    Dim hr, min, sec As Integer
    
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Timer1.Enabled = True
    End Sub
    
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Time.Text = Date.Now.ToString("h:mm:ss tt")
    End Sub
    
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    
        Start.Text = ""
        EndLbl.Text = ""
        Elapse.Text = ""
        Timer2.Enabled = True
        Start.Text = Time.Text
    End Sub
    
    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
    
        sec = sec + 1
        If (sec = 60) Then
            sec = 0
            min = min + 1
        ElseIf (min = 60) Then
            min = 0
            hr = hr + 1
        ElseIf (hr = 24) Then
            hr = 0
            min = 0
            sec = 0
        End If
    
        Elapse.Text = String.Format("{0}hr : {1}min : {2}sec", hr, min, sec)
        Timer2.Interval = 1000
    End Sub
    
    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Timer2.Enabled = False
        EndLbl.Text = Label4.Text
    
        hr = 0
        min = 0
        sec = 0
        Timer2.Interval = 1
    End Sub
    

    Credits to the starting code given by NeverHopeless. Thanks alot.

  • Marc Intes
    Marc Intes over 10 years
    it is working but how do i make it display only hour:minute:seconds. so it would fit my real-time clock.
  • NeverHopeless
    NeverHopeless over 10 years
    Updated code. Just noticed my code was a mixture of C# and VB.Net my bad :S
  • Marc Intes
    Marc Intes over 10 years
    thanks alot i have now created an accurate timer. i will be posting my code in my first post. Thanks for the starting code
  • igrimpe
    igrimpe over 10 years
    No, you haven't created an "accurate" timer (at least not if you took the above code). If you set a timers interval to 1000 ms, the timer will fire after APPROXIMATELY 1000 ms (thats 1000ms + some ms). Since you add 1 sec on each "tick", your "real time clock" will be too slow. You wont probably notice this if the measured time is small, but when it runs longer, you can see (or measure) the effect. The code below from George avoids this problem.
  • igrimpe
    igrimpe over 10 years
    Better code than "counting" seconds! Since timer events aren't reliable, one should never assume, that a 1000 ms timer fires after 1000 ms EXACTLY.
  • Marc Intes
    Marc Intes over 10 years
    Thank you this has improved and made the code more simple and accurate.
  • dbasnett
    dbasnett over 10 years
    I don't mind being down voted, but without a comment I can't fix/improve my answer.