Pause/Restart Countdown Timer Question

Please support our VB.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2006
Posts: 42
Reputation: ChadW is an unknown quantity at this point 
Solved Threads: 0
ChadW's Avatar
ChadW ChadW is offline Offline
Light Poster

Pause/Restart Countdown Timer Question

 
0
  #1
Nov 18th, 2006
I have a countdown timer in my application that is to be controlled via keyboard. Currently, it will start (or reset to the starting value of 20minutes) when the user presses "B", and stop when the user presses "A". Trying to figure out how to restart the timer from the stopped time. i.e. if the user presses "A" at timer 15 mins 32 seconds, pressing a again would resume the countdown. Here is the code I have currently (at least as it pertains to the countdown).

PrivateSub Me_KeyDown(ByVal sender AsObject, ByVal e As System.Windows.Forms.KeyEventArgs) HandlesMe.KeyDown
If e.KeyCode = Keys.B Then
Timer1.Enabled = True
CountDownStart = Microsoft.VisualBasic.DateAndTime.Timer
ElseIf e.KeyCode = Keys.A Then
Timer1.Enabled = False
End If
End Sub

Would someone be kind enough to point me in the right direction please?
Insert generic signature here
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 262
Reputation: Dukane is an unknown quantity at this point 
Solved Threads: 22
Dukane's Avatar
Dukane Dukane is offline Offline
Posting Whiz in Training

Re: Pause/Restart Countdown Timer Question

 
0
  #2
Nov 19th, 2006
Well, think of it this way. If the user presses A, you want the timer's enabled property to be the opposite of what it currently is.

In Visual Basic, there is a Not keyword which logically negates an expression.

So, you're pseudocode would probably look like this:

If (theButton was A) Then
timer.Enabled = Not Timer.Enabled

Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 42
Reputation: ChadW is an unknown quantity at this point 
Solved Threads: 0
ChadW's Avatar
ChadW ChadW is offline Offline
Light Poster

Re: Pause/Restart Countdown Timer Question

 
0
  #3
Nov 19th, 2006
Thanks for the suggestion. While that did in fact pause the countdown, the timer continued its counting even though it wasn't displaying it. I.e. when "A" was pressed, it did pause it, but if i waited for example 15 seconds while it was paused, and then pressed "A" again to resume the countdown, the count was actually exactly where it would have been if i hadn't pressed pause. Any ideas?

  1.  
  2. PrivateSub Me_KeyDown(ByVal sender AsObject, ByVal e As System.Windows.Forms.KeyEventArgs) HandlesMe.KeyDown
  3. If e.KeyCode = Keys.B Then
  4. Timer1.Enabled = True
  5. CountDownStart = Microsoft.VisualBasic.DateAndTime.Timer
  6. ElseIf e.KeyCode = Keys.A Then
  7. Timer1.Enabled = Not Timer1.Enabled
  8. End If
  9. End Sub
Insert generic signature here
Reply With Quote Quick reply to this message  
Join Date: Dec 2002
Posts: 461
Reputation: waynespangler is on a distinguished road 
Solved Threads: 56
waynespangler waynespangler is offline Offline
Posting Pro in Training

Re: Pause/Restart Countdown Timer Question

 
0
  #4
Nov 20th, 2006
Chad,
Try something along these lines.
  1. Private StartTime As Integer = 100
  2.  
  3. Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
  4. If e.KeyCode = Keys.A Then
  5. Timer1.Enabled = Not Timer1.Enabled
  6. e.Handled = True
  7. End If
  8. End Sub
  9.  
  10. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  11. Timer1.Interval = 1000
  12. Timer1.Enabled = True
  13. End Sub
  14.  
  15. Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
  16. StartTime -= 1
  17. Label1.Text = CStr(StartTime)
  18. End Sub
It counts down a variable called StartTime. It is done each second. When the timer is disabled then the countdown stops also.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the VB.NET Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC