Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Aug 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Problem with timers in visual basic

    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=UTyO6...ayer_embedded#!

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

  • #2
    New Coder
    Join Date
    Aug 2012
    Posts
    11
    Thanks
    0
    Thanked 1 Time in 1 Post
    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
    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 = number + 1
    End Sub

  • #3
    New to the CF scene
    Join Date
    Aug 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Vb.net right?
    Have you learned it yet, or only going off youtube tuts? lol
    Make this your new code
    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
    Last edited by bryson; 08-22-2012 at 05:02 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •