[RESOLVED] Timer vs array-VBForums
Results 1 to 10 of 10

Thread: [RESOLVED] Timer vs array

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2017
    Posts
    5

    Resolved [RESOLVED] Timer vs array

    Hi!
    I'm trying to print the contents of an array in a single label with an intervall of 5 seconds between every content. My code is not giving me the interval of 5 seconds...Anyone got a easy solution to fix this?

    Code:
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
       Timer1.Enabled = True
    End Sub
    
    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
       Timer1.Stop()
       For i As Integer = 0 To LastIndexOfArray
          Timer1.Interval = 5000
          Timer1.Start()
          Label1.Text = Array1(i)
          Timer1.Stop()
       Next i
       Timer1.Enabled = False
    End Sub
    Regards frankm

  2. #2
    Lively Member Goggy's Avatar
    Join Date
    Oct 2017
    Posts
    65

    Re: Timer vs array

    Hope this works a bit better for you.

    Code:
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
       Timer1.Enabled = True
       Timer1.Interval = 5000
       Timer1.Start()
    End Sub
    
    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    
       For i As Integer = 0 To LastIndexOfArray
          Label1.Text = Array1(i)
       Next i
    
    End Sub
    Regards frankm[/QUOTE]
    Utterly useless, but always willing to help

    As a finishing touch god created the dutch

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2017
    Posts
    5

    Re: Timer vs array

    I've tried this to, but it not giving the interval of 5 seconds between each content.

  4. #4
    Lively Member Goggy's Avatar
    Join Date
    Oct 2017
    Posts
    65

    Re: Timer vs array

    Did you clik the button and waited 5 seconds?

    Have tried the following code on my machine and it runs perfect, only difference i didnt use a button but run the timer from the form load event

    Code:
        Private WithEvents Timer1 As New System.Windows.Forms.Timer
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            With Timer1
                .Enabled = True
                .Interval = 5000
                .Start()
            End With
        End Sub
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            Debug.Print(Timer1.Interval.ToString)
        End Sub
    Utterly useless, but always willing to help

    As a finishing touch god created the dutch

  5. #5
    Fanatic Member
    Join Date
    Aug 2004
    Location
    Essex, UK
    Posts
    539

    Re: Timer vs array

    I think you need something more like this:

    Code:
    Public Class Form1
    
        Dim Array1() As Integer = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
        Dim iIndex As Integer
    
        Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
            If iIndex < Array1.Count Then
                Label1.Text = CStr(Array1(iIndex))
                iIndex += 1
            Else
                Timer1.Stop()
            End If
        End Sub
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Timer1.Interval = 5000
            Timer1.Start()
        End Sub
    
    End Class

  6. #6

    Thread Starter
    New Member
    Join Date
    Dec 2017
    Posts
    5

    Re: Timer vs array

    Quote Originally Posted by Goggy View Post
    Did you clik the button and waited 5 seconds?

    Have tried the following code on my machine and it runs perfect, only difference i didnt use a button but run the timer from the form load event
    Yes it waits 5 seconds, but there are no intervals in between the posting of the contents.

  7. #7

    Thread Starter
    New Member
    Join Date
    Dec 2017
    Posts
    5

    Re: Timer vs array

    Quote Originally Posted by paulg4ije View Post
    I think you need something more like this:

    Code:
    Public Class Form1
    
        Dim Array1() As Integer = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
        Dim iIndex As Integer
    
        Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
            If iIndex < Array1.Count Then
                Label1.Text = CStr(Array1(iIndex))
                iIndex += 1
            Else
                Timer1.Stop()
            End If
        End Sub
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Timer1.Interval = 5000
            Timer1.Start()
        End Sub
    
    End Class
    This could work, unless i have an array of different strings, not numbers from 1 to x.

  8. #8
    Lively Member Goggy's Avatar
    Join Date
    Oct 2017
    Posts
    65

    Re: Timer vs array

    Have you tried the code provided by paul4ije or me?
    Utterly useless, but always willing to help

    As a finishing touch god created the dutch

  9. #9
    Lively Member Goggy's Avatar
    Join Date
    Oct 2017
    Posts
    65

    Re: Timer vs array

    The code provided by paul4ije was an example, ofcourse you would have to make it fit your situation.
    Utterly useless, but always willing to help

    As a finishing touch god created the dutch

  10. #10

    Thread Starter
    New Member
    Join Date
    Dec 2017
    Posts
    5

    Re: Timer vs array

    Quote Originally Posted by frankm View Post
    This could work, unless i have an array of different strings, not numbers from 1 to x.
    Ahh sorry... tried the code from paul4ije now, and it works just like i want it. Thank you!

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.