944,047 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Unsolved
  • Views: 10451
  • VB.NET RSS
Nov 18th, 2006
0

Pause/Restart Countdown Timer Question

Expand Post »
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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
ChadW is offline Offline
42 posts
since Oct 2006
Nov 19th, 2006
0

Re: Pause/Restart Countdown Timer Question

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

Reputation Points: 45
Solved Threads: 28
Posting Whiz in Training
Dukane is offline Offline
282 posts
since Oct 2006
Nov 19th, 2006
0

Re: Pause/Restart Countdown Timer Question

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?

VB.NET Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Light Poster
ChadW is offline Offline
42 posts
since Oct 2006
Nov 20th, 2006
0

Re: Pause/Restart Countdown Timer Question

Chad,
Try something along these lines.
VB.NET Syntax (Toggle Plain Text)
  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.
Reputation Points: 84
Solved Threads: 58
Posting Pro in Training
waynespangler is offline Offline
461 posts
since Dec 2002

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: How to Use Foxpro Database in VB.Net 2005
Next Thread in VB.NET Forum Timeline: New user of vb.net





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC