|
-
Nov 30th, 2010, 01:17 AM
#1
Thread Starter
New Member
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...
-
Nov 30th, 2010, 01:22 AM
#2
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.
-
Nov 30th, 2010, 01:58 AM
#3
Thread Starter
New Member
Re: Timer Restart
Hate to sound dumb here, but can you give me an example on doing that correctly?
-
Nov 30th, 2010, 02:11 AM
#4
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.
-
Nov 30th, 2010, 06:06 AM
#5
Frenzied Member
Re: Timer Restart
you could call the button_click()
or simply set the timervalues and trun it on after the loop
-
Nov 30th, 2010, 01:03 PM
#6
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|