![]() ![]() | ||
When you add items to a list box, each item is given an index, and you can refer to the item in the list box with this index by using the Items property, like this: ListBox1.Items(5). The first item added to a list box gets the index 0, the next index 1, and so on. You also can get the index of an object in a list box with the IndexOf method, like this: ListBox1.Items.IndexOf(Object5).
When the user selects an item in a list box, you can get the selected item's index with the list box's SelectedIndex (formerly ListIndex) property. You also can get the selected item's corresponding object with the SelectedItem property. Here's an example where I display the index of an item the user has selected in the SelectedIndexChanged event of a list box:
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As _
System.Object, ByVal e As System.EventArgs) Handles _
ListBox1.SelectedIndexChanged
TextBox1.Text = "You selected item " & ListBox1.SelectedIndex + 1
End Sub
For more information, see "Determining Which List Box Items Are Selected" later in this chapter.
![]() ![]() | ||