This documentation is archived and is not being maintained.

ListBox.GetSelected Method

Returns a value indicating whether the specified item is selected.

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

public bool GetSelected(
	int index


Type: System.Int32

The zero-based index of the item that determines whether it is selected.

Return Value

Type: System.Boolean
true if the specified item is currently selected in the ListBox; otherwise, false.


The index parameter is less than zero or greater than or equal to the value of the Count property of the ListBox.ObjectCollection class.

You can use this method to quickly determine whether a specified item is selected. This method is useful when a specific operation needs to be performed when a specific item in a multiple-selection ListBox is selected.

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.

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