| | |
Pause/Restart Countdown Timer Question
Please support our VB.NET advertiser: Intel Parallel Studio Home
![]() |
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).
Would someone be kind enough to point me in the right direction please?
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
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
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
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)
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 = Not Timer1.Enabled End If End Sub
Insert generic signature here
•
•
Join Date: Dec 2002
Posts: 461
Reputation:
Solved Threads: 56
Chad,
Try something along these lines.
It counts down a variable called StartTime. It is done each second. When the timer is disabled then the countdown stops also.
Try something along these lines.
VB.NET Syntax (Toggle Plain Text)
Private StartTime As Integer = 100 Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp If e.KeyCode = Keys.A Then Timer1.Enabled = Not Timer1.Enabled e.Handled = True End If End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Timer1.Interval = 1000 Timer1.Enabled = True End Sub Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick StartTime -= 1 Label1.Text = CStr(StartTime) End Sub
![]() |
Similar Threads
- Timer question: Scoreboard (VB.NET)
- Tkinter Countdown Timer problems. (Python)
- Countdown Timer (VB.NET)
Other Threads in the VB.NET Forum
- Previous Thread: How to Use Foxpro Database in VB.Net 2005
- Next Thread: New user of vb.net
| Thread Tools | Search this Thread |
.net .net2008 2005 2008 access account array basic beginner bing browser button buttons center check code crystalreport cuesent data database datagrid datagridview date datetimepicker designer dissertation dissertations dissertationtopic dropdownlist excel fade file-dialog filter forms ftp generatetags google hardcopy html images input insert intel internet mobile monitor ms net networking objects open output panel passingparameters pdf picturebox picturebox1 port position print printing problem save searchbox searchvb.net select serial settings shutdown soap sqlserver survey table tcp temperature text textbox timer timespan toolbox transparency trim update user vb vb.net vb.netformclosing()eventpictureboxmessagebox vb2008 vba vbnet view visual visualbasic.net visualstudio.net visualstudio2008 web winforms wpf wrapingcode year





