Visual Basic .NET - ListBox Control

The VB.NET ListBox control is used to display a list of items to the user, where the user can select one or more items. 

Properties:

  • Items: Gets or sets the items in the ListBox.
  • SelectedItem: Gets or sets the currently selected item in the ListBox.
  • SelectionMode: Gets or sets the selection mode of the ListBox.

Methods:

  • Add: Adds an item to the end of the ListBox.
  • Remove: Removes an item from the ListBox.
  • Clear: Removes all items from the ListBox.

Events:

  • SelectedIndexChanged: Occurs when the selected item in the ListBox is changed.
  • DoubleClick: Occurs when the ListBox is double-clicked.
' Create a new ListBox control
Dim lstNames As New ListBox()
' Set some properties
lstNames.Items.Add("John")
lstNames.Items.Add("Mary")
lstNames.Items.Add("Tom")
lstNames.SelectionMode = SelectionMode.MultiSimple
' Add the ListBox to the form
Me.Controls.Add(lstNames)
' Handle the SelectedIndexChanged event
Private Sub lstNames_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles lstNames.SelectedIndexChanged
    ' Display the selected items
    For Each item In lstNames.SelectedItems
        MessageBox.Show(item.ToString())
    Next
End Sub

This example creates a new ListBox control and sets some properties, including adding items to the ListBox and setting the selection mode. It then adds the ListBox to the form and handles the SelectedIndexChanged event, which displays the selected items in a message box when the user changes the selection.