|
|
Title | Select items in a ListBox in code in Visual Basic .NET |
Description | This example shows how to select items in a ListBox in code in Visual Basic .NET. |
Keywords | ListBox, select, SelectedItem, SelectedIndex, SetSelected, VB.NET |
Categories | VB.NET, Controls |
|
|
For a ListBox that allows single selection, either set the SelectedIndex or the SelectedItem property.
For a ListBox that allows multiple selection, use the SetSelected method to selected or deselect items.
|
|
' Select item 4 in ListBox1.
' (Alternatively set ListBox1.SelectedItem.)
ListBox1.SelectedIndex = 4
'Or do this: ListBox1.SelectedItem = ListBox1.Items(4)
' Select every third item in ListBox2.
For i As Integer = 1 To ListBox2.Items.Count - 1 Step 3
ListBox2.SetSelected(i, True)
Next i
|
|
For more information on programming in VB .NET, see my book Visual Basic 2005 Programmer's Reference.
|
|
|
|
|
|