Hi, I'm getting quite good with vb.net now and have created quite a few good programs but I'm having trouble with something that seems simple but i cant figure out whats wrong, if anybody can help me with the following code it would be very appreciated.

What I'm trying to do is create a timer where you put in the time (in seconds) into textbox1 and when you click button2, label2.Text keeps decreasing by 1 each time timer2 has an interval, but for some reason, when i click start, it only goes down 1 and then just stops on that number.

Dim myTime As Integer

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Button1.Text = "Start" Then
            myTime = TextBox1.Text
            Label2.Text = myTime
            Timer2.Start()
            Button1.Text = "Stop"
        Else
            If Button1.Text = "Stop" Then
                Timer2.Stop()
                Button1.Text = "Start"
            End If
        End If
    End Sub
    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
        Label2.Text = myTime - 1
    End Sub

    Private Sub Form8_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Timer2.Enabled = True
        Timer2.Stop()
    End Sub

Please help me solve this, thanks.

I changed

Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick 
Label2.Text = myTime - 1 
End Sub

To

Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick 
Label2.Text = myTime - 1 
myTime = Label2.Text 
End Sub

Since you want something to continue operating, i would switch from an if statement to some kind of while loop, so that as long as Stop isn't pressed, it will continue to operate. Also, you are consistently subtracting 1 from a fixed value (mytime) when in fact one of the numbers should be updating with the timer.

Summary:

Use a while loop so your countdown timer continues to operate until a given condition is met, say, when it reaches zero or something.

Check the math so that you aren't subtracting 1 from an unchanging value.

Hope this helps!
Halbo

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.