[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
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:
myLabel.Text = myArray(index)
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.
Re: Display list rolling items