ListBox.SelectedIndex Property


Gets or sets the zero-based index of the currently selected item in a ListBox.

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

override SelectedIndex : int with get, set

Property Value

Type: System.Int32

A zero-based index of the currently selected item. A value of negative one (-1) is returned if no item is selected.

Exception Condition

The assigned value is less than -1 or greater than or equal to the item count.


The SelectionMode property is set to None.

For a standard ListBox, you can use this property to determine the index of the item that is selected in the ListBox. If the SelectionMode property of the ListBox is set to either SelectionMode.MultiSimple or SelectionMode.MultiExtended (which indicates a multiple-selection ListBox) and multiple items are selected in the list, this property can return the index to any selected item.

To retrieve a collection containing the indexes of all selected items in a multiple-selection ListBox, use the SelectedIndices property. If you want to obtain the item that is currently selected in the ListBox, use the SelectedItem property. In addition, you can use the SelectedItems property to obtain all the selected items in a multiple-selection ListBox.

The following code example demonstrates how to use the SelectedIndex property with the TopIndex property to move the currently selected item to the top of the list of items in the display area of the ListBox. The example further demonstrates how to remove items using the RemoveAt method of the System.Windows.Forms.ListBox.ObjectCollection class, and how to clear all item selection using the ClearSelected method. The code first moves the currently selected item in the ListBox to the top of the list. The code then removes all items before the currently selected item and clears all selections in the ListBox. This example requires that a ListBox containing items is added to a form and that an item is currently selected in the ListBox.

No code example is currently available or this language may not be supported.

.NET Framework
Available since 1.1
Return to top