This documentation is archived and is not being maintained.

ListBox.SelectedIndices Property

Gets a collection that contains the zero-based indexes of all currently selected items in the ListBox.

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

public ListBox..::..SelectedIndexCollection SelectedIndices { get; }

Property Value

Type: System.Windows.Forms.ListBox.SelectedIndexCollection
A ListBox.SelectedIndexCollection containing the indexes of the currently selected items in the control. If no items are currently selected, an empty ListBox.SelectedIndexCollection is returned.

For a multiple-selection ListBox, this property returns a collection containing the indexes to all items that are selected in the ListBox. For a single-selection ListBox, this property returns a collection containing a single element containing the index of the only selected item in the ListBox. For more information about how to manipulate the items of the collection, see ListBox.SelectedIndexCollection.

The ListBox class provides a number of ways to reference selected items. Instead of using the SelectedIndices property to obtain the index position of the currently selected item in a single-selection ListBox, you can use the SelectedIndex property. If you want to obtain the item that is currently selected in the ListBox, instead of the index position of the item, use the SelectedItem property. In addition, you can use the SelectedItems property if you want to obtain all the selected items in a multiple-selection ListBox.

The following code example demonstrates how to use the FindString method to search for all instances of the search text in the items of the ListBox. The example uses the version of the FindString method that enables you to specify a starting search index from which to do a continual search of all items in the ListBox. The example also demonstrates how to determine when the FindString method begins searching from the top of the list after it reaches the bottom of the list of items to prevent a recursive search. Once items are found in the ListBox, they are selected using the SetSelected method.

private void FindAllOfMyString(string searchString)
   // Set the SelectionMode property of the ListBox to select multiple items.
   listBox1.SelectionMode = SelectionMode.MultiExtended;

   // Set our intial index variable to -1.
   int x =-1;
   // If the search string is empty exit.
   if (searchString.Length != 0)
      // Loop through and find each item that matches the search string.
         // Retrieve the item based on the previous index found. Starts with -1 which searches start.
         x = listBox1.FindString(searchString, x);
         // If no item is found that matches exit.
         if (x != -1)
            // Since the FindString loops infinitely, determine if we found first item again and exit.
            if (listBox1.SelectedIndices.Count > 0)
               if(x == listBox1.SelectedIndices[0])
            // Select the item in the ListBox once it is found.

      }while(x != -1);

.NET Framework

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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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