SelectionMode Enumeration
Collapse the table of content
Expand the table of content

SelectionMode Enumeration

Specifies the selection behavior of a list box.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

<ComVisibleAttribute(True)> _
Public Enumeration SelectionMode
Dim instance As SelectionMode

Member nameDescription
NoneNo items can be selected.
OneOnly one item can be selected.
MultiSimpleMultiple items can be selected.
MultiExtendedMultiple items can be selected, and the user can use the SHIFT, CTRL, and arrow keys to make selections

This enumeration is used by classes such as ListBox and CheckedListBox.

The following example demonstrates how to use the GetSelected method to determine which items in a ListBox are selected in order to select the items that are not selected and deselect the items that are selected. The example also demonstrates using the SelectionMode property to enable a ListBox to have more than one selected item and uses the Sorted property to demonstrate how to sort items in a ListBox automatically. This example assumes that a ListBox, named listBox1, has been added to a form and that the InitializeMyListBox method defined in the example is called from the Load event of the form.

Private Sub InitializeMyListBox()
   ' Add items to the ListBox.

   ' Sort all items added previously.
   listBox1.Sorted = True 

   ' Set the SelectionMode to select multiple items.
   listBox1.SelectionMode = SelectionMode.MultiExtended

   ' Select three initial items from the list.
   listBox1.SetSelected(0, True)
   listBox1.SetSelected(2, True)
   listBox1.SetSelected(4, True)

   ' Force the ListBox to scroll back to the top of the list.
   listBox1.TopIndex = 0
End Sub 

Private Sub InvertMySelection()

   Dim x As Integer 
   ' Loop through all items the ListBox. 
   For x = 0 To listBox1.Items.Count - 1

      ' Determine if the item is selected. 
      If listBox1.GetSelected(x) = True Then 
         ' Deselect all items that are selected.
         listBox1.SetSelected(x, False)
         ' Select all items that are not selected.
         listBox1.SetSelected(x, True)
      End If 
   Next x
   ' Force the ListBox to scroll back to the top of the list.
   listBox1.TopIndex = 0
End Sub

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

Community Additions

© 2016 Microsoft