LISTBOX EXAMPLE
The Listbox control displays a list of information (such as a shopping list or city list in an address book
program). Each individual item in the list can be checked to indicate that item has been selected.
Multiple items in the list can be selected as well.
The most common event for Listbox control is the SelectedIndexChanged event.
Consider the form design below:
Double click any place on the form (not in the listbox) to open the code window and type
the following code on the “Form_Load” subroutine.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles [Link]
[Link]("List item 1")
[Link]("List item 2")
End Sub
Next double click on the listbox to open the code window and type the following code:
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles_
[Link]
MsgBox("Selected index = " & [Link])
MsgBox("Selected item = " & [Link])
End Sub
When you run above program and select the first element in the Listbox, the following
output appears.
1
When you click “OK” in the message box above, the following output appears.