ListBox.SelectionMode Property


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Gets or sets the method in which items are selected in the ListBox.

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

public virtual SelectionMode SelectionMode { get; set; }

Property Value

Type: System.Windows.Forms.SelectionMode

One of the SelectionMode values. The default is SelectionMode.One.

Exception Condition

The assigned value is not one of the SelectionMode values.

The SelectionMode property enables you to determine how many items in the ListBox a user can select at one time and how the user can make multiple-selections. When the SelectionMode property is set to SelectionMode.MultiExtended, pressing SHIFT and clicking the mouse or pressing SHIFT and one of the arrow keys (UP ARROW, DOWN ARROW, LEFT ARROW, and RIGHT ARROW) extends the selection from the previously selected item to the current item. Pressing CTRL and clicking the mouse selects or deselects an item in the list. When the property is set to SelectionMode.MultiSimple, a mouse click or pressing the SPACEBAR selects or deselects an item in the list.

The following code 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 requires 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 void 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.

   // Force the ListBox to scroll back to the top of the list.

private void InvertMySelection()
   // Loop through all items the ListBox.
   for (int x = 0; x < listBox1.Items.Count; x++)
      // Determine if the item is selected.
      if(listBox1.GetSelected(x) == true)
         // Deselect all items that are selected.
         // Select all items that are not selected.
   // Force the ListBox to scroll back to the top of the list.

.NET Framework
Available since 1.1
Return to top