4

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
  • 737
  • 9
  • 25
  • 51
  • Possibly you need to schedule a Timer instance after each second and in Timer_Tick event update label. For every 60 sec minutes will be increased and so on. Once button stop pressed set `Timer.Enabled = false`. – NeverHopeless Sep 17 '13 at 05:54
  • yes that is what i want to do but my problem is how do i display it in able so the format would be hours:minutes:seconds – Marc Intes Sep 17 '13 at 06:06

3 Answers3

9

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

George
  • 284
  • 1
  • 2
  • 7
  • 1
    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. – igrimpe Sep 17 '13 at 11:38
3

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
dbasnett
  • 11,334
  • 2
  • 25
  • 33
1

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.

NeverHopeless
  • 11,077
  • 4
  • 35
  • 56
  • it is working but how do i make it display only hour:minute:seconds. so it would fit my real-time clock. – Marc Intes Sep 17 '13 at 06:40
  • Updated code. Just noticed my code was a mixture of C# and VB.Net my bad :S – NeverHopeless Sep 17 '13 at 06:57
  • thanks alot i have now created an accurate timer. i will be posting my code in my first post. Thanks for the starting code – Marc Intes Sep 17 '13 at 07:13
  • 1
    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 Sep 17 '13 at 11:34