VB.net real-time time elapsed feature

Marc Intes picture Marc Intes · Sep 17, 2013 · Viewed 30.3k times · Source

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.

Answer

George picture George · Sep 17, 2013

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