Basically what I want is to have the timer be reset back to 3 sec once the Reset button is clicked. Preferably, I want the code to be in the same sub as the timer, though any better ideas are most welcomed. I've already put in comments where i want the code to be... thanks in advance for your help :)

Here's the part of the code...

Private Sub Timer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer.Tick
        Static CountDown As New TimeSpan(0, 1, 0)
        Dim Time0 As New TimeSpan(0, 0, 0)

        CountDown = CountDown.Subtract(New TimeSpan(0, 0, 1))
        If CountDown.Equals(Time0) Then
            Timer.Stop()
            CountDown = New TimeSpan(0, 3, 0)
        End If

        lblTimer.Text = CountDown.ToString

        If CountDown <= New TimeSpan(0, 0, 30) Then
            If lblTimer.ForeColor = Color.White Then
                lblTimer.ForeColor = Color.Red
            Else
                lblTimer.ForeColor = Color.White
            End If
        End If

        'check if btnReset is clicked
End Sub

Recommended Answers

All 4 Replies

Move your static countdown variable to form level.
In the btnReset click event, assign the new time value of 3 secons to your form level variable (m_CountDown)
And re-enable your timer if necessary

erm... how do i do tt? I tried tt and had an error... " 'Static' is not a valid on a member variable declaration"

The variable will no longer need to be "Static" since its scope is in existence for the life of the form itself. This would be refered to as a module level variable (hence the "m_" prefix to signify its scope) or form level.

Public Class Form1

Dim m_CountDown As New TimeSpan(0, 1, 0)

Private Sub Form1_Load()
...
End Sub
End Class

The variable will no longer need to be "Static" since its scope is in existence for the life of the form itself. This would be refered to as a module level variable (hence the "m_" prefix to signify its scope) or form level.


Public Class Form1

Dim m_CountDown As New TimeSpan(0, 1, 0)

Private Sub Form1_Load() ... End Sub End Class[Code=VB]

Public Class Form1

Dim m_CountDown As New TimeSpan(0, 1, 0)

Private Sub Form1_Load()
...
End Sub
End Class

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.