Display items in a label - one at a time

kasteel

Well-known member
Joined
May 29, 2009
Messages
50
Programming Experience
10+
Hi

I have 10 items in a listbox. (Just text) How can I display one item (text) at a time from the listbbox in a label every time a user clicks on the NEXT button. (The label will just display a new row of the listbox every time a user clicks on Next)

Any help would be appreciated.

Thanks
 
By default, the SelectedIndex of a ListBox is -1, so you need to set it to 0 to start with the first item. Then, increment it by one each time the button is clicked.
VB.NET:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load  
   ListBox1.SelectedIndex = 0 'select first item in listbox
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Label1.Text = ListBox1.Text  'display selected item
    ListBox1.SelectedIndex = ListBox1.SelectedIndex + 1  'select next item in the list
End Sub
NOTE: There is one thing I did not do here. You will receive an error when the program hits that last item in the listbox and you attempt to increment the selectedindex.

Your code needs to accommodate that.
 
Back
Top