Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27554

How do I remove these extra blank entries in a listbox?..

$
0
0
I have the following code in my program which adds all the items from a .txt to a listbox when the form loads.
Code:

        Dim words As String = My.Computer.FileSystem.ReadAllText("location blah blah.txt")    ' reads file to listbox and knows each item is on a new line
        Dim wordssplitter As String() = words.Split(vbNewLine)
        ListBox1.Items.AddRange(wordssplitter)

I later use the following code in a button to generate random entries from the listbox into a locked textbox
Code:

        Dim question1 As Long
        Randomize()
        question1 = Int(Rnd() * ListBox1.Items.Count)    'gets one random item from list / puts in label/ removes it
        TextBox1.Text = ListBox1.Items(question1)
        ListBox1.Items.RemoveAt(question1)

My problem is that when I load the file into the listbox - it seems to add 2 "empty clickable options" if you understand..
so when I go to generate one, it sometimes fills the box with one of the empty options.


Can someone give me a line or two of code that will remove any empty options
- I've also check the .txt file and it doesn't have any blank lines that I can see!
Thanks!

Viewing all articles
Browse latest Browse all 27554

Trending Articles