Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleSelect items in a ListBox in code in Visual Basic .NET
DescriptionThis example shows how to select items in a ListBox in code in Visual Basic .NET.
KeywordsListBox, select, SelectedItem, SelectedIndex, SetSelected, VB.NET
CategoriesVB.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.
 
 
Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated