Results 1 to 2 of 2

Thread: How to reset timer

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2017
    Posts
    110

    Exclamation How to reset timer

    Hi, I'm trying to reset a timer on button click from another form, so when it opens the form where the timer is, it will restart on the number of minutes saved in my database. I was able to restart the timer but it only resumes the time and not resets it. Here's my code that I put in my module:

    Code:
    Public TargetDT As DateTime
    
        Public Sub RestartTimer()
    
            Dim CountDownFrom As TimeSpan = TimeSpan.FromMinutes(frmTakeTest.lblTimer.Text)
    
            frmTakeTest.Timer1.Interval = 500
            TargetDT = DateTime.Now.Add(CountDownFrom)
            frmTakeTest.Timer1.Start()
        End Sub
    and here's what's on the form where the timer is:

    Code:
     Dim TargetDT As DateTime
        Private CountDownFrom As TimeSpan = TimeSpan.FromMinutes(0)
    
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            Dim ts As TimeSpan = TargetDT.Subtract(DateTime.Now)
            If ts.TotalMilliseconds > 0 Then
                Me.lblTT_Timer.Text = getTimeString(ts)
            Else
                Me.lblTT_Timer.Text = getTimeString(ts)
                Timer1.Stop()
            End If
        End Sub
    
        Private Function getTimeString(ByVal ts As TimeSpan) As String
            Return (ts.Hours.ToString("00") + ":" + ts.Minutes.ToString("00") _
            + ":" + ts.Seconds.ToString("00"))
        End Function

    Any ideas?

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: How to reset timer

    It looks like you have two definitions for TargetDT, one on each form.
    Why not make TargetDT on your frmTakeTest public, remove the one from where you restart the timer and change your code to reference the public one on your form.
    Code:
            frmTakeTest.TargetDT = DateTime.Now.Add(CountDownFrom)
            frmTakeTest.Timer1.Start()
    Or, if the RestartTimer reset code and the Public TargetDT definition are really in a Module, making it globally visible, then remove the local definition in the Form code so that it reference the one in the Module.
    Last edited by passel; Apr 4th, 2018 at 06:01 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width