User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the VB.NET section within the Software Development category of DaniWeb, a massive community of 456,507 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,663 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our VB.NET advertiser: Programming Forums
Views: 1227 | Replies: 2
Reply
Join Date: Sep 2007
Posts: 35
Reputation: Webbsta is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
Webbsta Webbsta is offline Offline
Light Poster

Help with countdown timer

  #1  
Sep 28th, 2007
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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2007
Posts: 35
Reputation: Webbsta is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
Webbsta Webbsta is offline Offline
Light Poster

Re: Help with countdown timer

  #2  
Sep 28th, 2007
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
Reply With Quote  
Join Date: Sep 2007
Posts: 9
Reputation: Halbo is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Halbo Halbo is offline Offline
Newbie Poster

Solution Re: Help with countdown timer

  #3  
Oct 1st, 2007
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
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb VB.NET Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the VB.NET Forum

All times are GMT -4. The time now is 3:42 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC