Results 1 to 6 of 6

Thread: Timer Restart

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2010
    Posts
    2

    Timer Restart

    I know this has been asked previously, but I can't seem to find an answer that fits what I'm looking for.

    Need to have a timer reset and start counting down again automatically. I would like to use a loop to do it if possible, as I will be using the loop count later in the program. Here is what I have so far, the counter rolls back to zero but I can't seem to find a way to get it to restart again. For testing I set the time to .1 but it will be set for 2 hours (120) when its working.

    Public Class Escaltion_Timer
    Dim myTime As Integer
    Private alarmTime As Date


    Private Sub ButtonStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonStart.Click
    Me.Text = TextBox1.Text
    Timer1.Enabled = True
    Me.alarmTime = Date.Now.AddMinutes(0.1)
    End Sub
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    Dim i As Integer
    If alarmTime < Date.Now Then
    Me.Timer1.Stop()
    MessageBox.Show("Next Escalation")
    Do Until i = 5
    i = i + 1
    Loop
    Else
    Dim remainingTime As TimeSpan = Me.alarmTime.Subtract(Date.Now)
    Me.LabelStage.Text = String.Format("{0}:{1:d2}:{2:d2}", _
    remainingTime.Hours, _
    remainingTime.Minutes, _
    remainingTime.Seconds)
    End If
    LabelProgress.Text = i
    End Sub
    End Class


    The loop sits at 0 like I want until the messagebox comes up and then instead of the process restarting with loop of 1 and counting down, clicking ok on the message box causes the loop to go straight to 5 and the program ends. If I click on the start button again however, everything does restart. I appreciate any suggestions. Please keep in mind I am completely new to this so try to keep it simple for me...

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Timer Restart

    How do you start it in the first place? You execute the code in your ButtonStart_Click method, right? So, how would you start it again? You'd execute the same code. Simply take that code out of the event handler and put it into it's own method. You can then call that method from the Click and the Tick event handlers. You would call it from the Tick event handler when the remaining time is less than or equal to zero.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2010
    Posts
    2

    Re: Timer Restart

    Hate to sound dumb here, but can you give me an example on doing that correctly?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Timer Restart

    A method is just a Sub or Function. You just have to declare a method, move the code into it and then write a line to call it.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,470

    Re: Timer Restart

    you could call the button_click()

    or simply set the timervalues and trun it on after the loop

  6. #6
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Re: Timer Restart

    What is the purpose of your Do ... Loop? It just counts from 1 to 5 with executing anything inside it, which will take a fraction of a millisecond. I don't think you have stated your problem clearly enough. Also, you should use the Code tags to make your code easier to read.

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