So I've been trying for about a month or so but been busy recently so couldn't address it then. Here is the code.

Public Class Form1
    Dim CurrentTime As DateTime
 
    Private Sub ButtonStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonStart.Click
        ButtonStart.Enabled = False
        ButtonPause.Enabled = True
 
        CurrentTime = DateTime.Now()
    End Sub
 
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If Timer1.Enabled = False Then
            ' Pause timer, since timer keeps accumulating, need to find a way to subtract the time gathered when the pause started until pause ends (determined by user).
        Else
            Dim CurrentTimeElapsed As TimeSpan = DateTime.Now.Subtract(CurrentTime)
 
            Label1.Text = CurrentTimeElapsed.Hours.ToString.PadLeft(2, "0"c) + ":" + CurrentTimeElapsed.Minutes.ToString.PadLeft(2, "0"c) + ":" + CurrentTimeElapsed.Seconds.ToString.PadLeft(2, "0"c) + "." + CurrentTimeElapsed.Milliseconds.ToString.PadLeft(3, "0"c)
        End If
    End Sub
 
    Private Sub ButtonPause_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonPause.Click
        If Timer1.Enabled = False Then
            ButtonPause.Text = "Pause"
            Timer1.Enabled = True
        Else
            ButtonPause.Text = "Resume"
            Timer1.Enabled = False
        End If
    End Sub
End Class

I know that even when the timer is disabled, it still keeps on accumulating so I need a way that when the timer is paused, the timer doesn't keep on accumulating and when the timer is resumed, the timer continues from where it was when it was paused. Any help is greatly appreciated.

Member Avatar for Unhnd_Exception

Heres a way

Public Class Form1
    Dim StartTime As DateTime 'old current time
    Dim PauseTime As DateTime 'keep track of the time when the timer is paused
    Dim TotalTimePaused As TimeSpan 'The total amount of time paused

    Private Sub ButtonStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonStart.Click
        ButtonStart.Enabled = False
        ButtonPause.Enabled = True
        StartTime = DateTime.Now()
        Timer1.Start()
    End Sub

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

        'Subtract the current "Now" time from the start time
        'and then subtract the total time paused
        Dim ElapsedTime As TimeSpan = DateAndTime.Now.Subtract(StartTime).Subtract(TotalTimePaused)
        Label1.Text = ElapsedTime.Hours.ToString.PadLeft(2, "0"c) + ":" + ElapsedTime.Minutes.ToString.PadLeft(2, "0"c) + ":" + ElapsedTime.Seconds.ToString.PadLeft(2, "0"c) + "." + ElapsedTime.Milliseconds.ToString.PadLeft(3, "0"c)

        'And if you wanted the total elapsed time from time of start then
        'subtract the current "Now" time from the start time only.
    End Sub

    Private Sub ButtonPause_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonPause.Click
        If Timer1.Enabled = False Then
            ButtonPause.Text = "Pause"
            'Add to the total time paused; the current time minus the 
            'time of pause.  this will be the total amount of time
            'paused so you can subtract it from the total amount of
            'time since the start button was pressed
            TotalTimePaused = TotalTimePaused.Add(DateAndTime.Now.Subtract(PauseTime))
            Timer1.Enabled = True
        Else
            ButtonPause.Text = "Resume"
            Timer1.Enabled = False
            'Set the pause time so it can be
            'calculated to the total time paused.
            'when the timer is resumed
            PauseTime = DateAndTime.Now
        End If
    End Sub

End Class

Thanks for the response, it works perfectly now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.