I have a ListBox Containg a list of items,i am wondering how would i create a handler which can Iterate the ListBox whenever an event is happen.
I have the Following Code to read a file into a listbox.
Private Sub Load_File_To_ListBox(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Load_File_To_ListBox.Click
Dim r As New IO.StreamReader("C:\Users\resu\Desktop\test.txt")
While (r.Peek() > -1)
lb1.Items.Add(r.ReadLine)
End While
r.Close()
End Sub
Here is my event handler code:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
TextBox1.Text = " "
TextBox1.Text &= ListBox1.SelectedItems.Item(i).ToString
i = i + 1
End Sub
i is declared as a global variable to keep track of the next item In the ListBox. What I want is to read the next item from the Listbox and put it into the TextBox whenever Button2 is Clicked.
Kindly help me in modifying my code to make it work.
If I understand correctly, your problem lies with the following line of code:
'This line of code looks at all of the items that have been
'selected in the list box, and out of all of the selected
'items it will select the item at index i.
TextBox1.Text &= ListBox1.SelectedItems.Item(i).ToString
Because the code is looking at selected items only and not all items the code is not behaving as you would expect. Instead, use the following line of code:
TextBox1.Text &= ListBox1.Items(i).ToString()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With