Results 1 to 3 of 3

Thread: [RESOLVED] Display list rolling items

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2017
    Location
    Italy
    Posts
    170

    Resolved [RESOLVED] Display list rolling items

    I have a text file with some news, I want to show them for a second, when I get to the last one I want to go back to the first one. Can you code it better than this ?:
    Code:
    Public Class Form1
        Dim mylist As ArrayList = New ArrayList()
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            InitializeTimer()
            populate()
        End Sub
    
        Private Sub InitializeTimer()
            Timer1.Interval = 1000
            Timer1.Enabled = True
            Button1.Text = "Stop"
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            If Button1.Text = "Stop" Then
                Button1.Text = "Start"
                Timer1.Enabled = False
            Else
                Button1.Text = "Stop"
                Timer1.Enabled = True
            End If
        End Sub
    
        Private Sub Timer1_Tick(ByVal Sender As Object, ByVal e As EventArgs) Handles Timer1.Tick
            Dim n As Integer = mylist.Count - 1
            Dim elemento As String = mylist(0).ToString
            Label1.Text = elemento
            mylist.RemoveAt(0)
            mylist.Insert(n, elemento)
        End Sub
    
        Sub populate()
            mylist.Clear()
            Dim fname As String = "F:\Documents\Visual Studio 2017\Projects\Roll.txt"
            Dim lines() = IO.File.ReadAllLines(fname)
            For Each line In lines
                mylist.Add(line)
            Next
        End Sub
    End Class
    Last edited by patel45; Feb 15th, 2018 at 06:49 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    Re: Display list rolling items

    Just at a glance, don't use an ArrayList... for anything... ever. If you did need a collection then you'd use a List(Of String) but you don't need a collection at all. Just use the array that you get back from ReadAllLines. Instead of removing and inserting items, just keep an index of the current item that wraps at the end.
    vb.net Code:
    1. myLabel.Text = myArray(index)
    2. index = (index + 1) Mod myArray.Length
    Initialise the index to zero and that code will wrap it back to zero when you go past the last element.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2017
    Location
    Italy
    Posts
    170

    Re: Display list rolling items

    Thanks Jmc, very simple.

Posting Permissions

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



Click Here to Expand Forum to Full Width