...

View Full Version : Problem with timers in visual basic



Swanton007
08-19-2012, 02:11 AM
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim number As Integer = 0
ListBox1.Items.Add(number)
number = number + 1
End Sub

Im expecting this to create something similar to this:
http://www.youtube.com/watch?v=UTyO6KiPJDw&feature=player_embedded#!

But it's not. Instead it keeps on outputting 0's into the listbox. Any ideas?

sh4rd
08-19-2012, 08:32 AM
Every time the timer ticks it is creating a new variable called number set to 0 then printing it. it is overwriting the existing variable, thus it remains at 0.
Try moving the variable declaration to outside the function


Dim number As Integer = 0

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
ListBox1.Items.Add(number)
number = number + 1
End Sub

bryson
08-22-2012, 04:56 AM
Vb.net right?
Have you learned it yet, or only going off youtube tuts? lol
Make this your new code


Dim number As Integer = 0

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
ListBox1.Items.Add(number)
number = += 1
End Sub



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum