Hi

I'm looking to write the contents of a text file to an array and view one line at a time in a listbox. When I say one line at a time I mean only view one line in the listbox and as It loops through the text file the line that is showing is replaced with the next line, and so on.
Sort of like a Tip of the day lisbox.

Tony

Recommended Answers

All 7 Replies

Try this. I would personally use a label instead of a listbox.

Public Class Form1
    Dim ary() As String
    Dim index As Integer = 0

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim str As String = My.Computer.FileSystem.ReadAllText("c:\test.txt")
        ary = Split(str, vbNewLine)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ListBox1.Items.Clear()
        ListBox1.Items.Add(ary(index))
        index += 1
        If index = UBound(ary) Then index = 0
    End Sub

End Class

Hi wayne

The lines are not apperaing in the listbox properly. Like word wrap not being checked in a text file

result:

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

Desired result:

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

Also is there a way to show the lines in the listBox at line 10 instead of the first line in the listbox? Should I maybe use a RT box instead? Not sure what you mean by using a label instead of a listbox? Can you view data in a label?

Tony

The label uses less resources than the others. A listbox can't (as far as I know) word wrap. A richtextbox is over kill.
Put a label on your form and set the AutoSize to false. Now size it to the area you want the text to show in and when the end of the line is reached it continues on the next line.

Hi Wayne

I've decided to go with a label instead. How would you cahnge teh following to list in a label:

ListBox1.Items.Clear()
ListBox1.Items.Add(ary(index))

Tony

Hi wayne

I've figured it out. The label was definitely the way to go! You have been very helpful! Now If I can only figure out how to save the checkbox setting to load the Tip of the day Form when my app starts if checked.

Thanks

Tony

In solution explorer double click on the MyProject. When the page loads select the Application tab. Look for the check box titled "Save My.Settings on shutdown" and make sure it is checked. Click on the Settings tab. You will be looking at a grid view. Click in the first box and give your setting a name (below I called it "ckSetting", name it whatever you like). In the next box click on the dropdown and select "Boolean" type. Leave the next set to "User". Leave the next one blank.
Every time your program is shutdown the setting is saved and reloaded when program started.
Now the code:

Public Class Form1

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        My.Settings.ckSetting = CheckBox1.Checked
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        CheckBox1.Checked = My.Settings.ckSetting
    End Sub
End Class

Hi Wayne

Thanks it works great! How would you call the Tip of the day form from the main form if the checkbox1.checked?

Tony

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.