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?

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

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?

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

Chad,
Try something along these lines.

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

It counts down a variable called StartTime. It is done each second. When the timer is disabled then the countdown stops also.

This article has been dead for over six months. Start a new discussion instead.