Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

ListBox.SelectedIndexCollection Class

Represents the collection containing the indexes to the selected items in a ListBox.

System.Object
  System.Windows.Forms.ListBox.SelectedIndexCollection

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)
public class SelectedIndexCollection : IList, 
	ICollection, IEnumerable

The ListBox.SelectedIndexCollection type exposes the following members.

  NameDescription
Public methodListBox.SelectedIndexCollectionInitializes a new instance of the ListBox.SelectedIndexCollection class.
Top
  NameDescription
Public propertyCountGets the number of items in the collection.
Public propertyIsReadOnlyGets a value indicating whether the collection is read-only.
Public propertyItemGets the index value at the specified index within this collection.
Top
  NameDescription
Public methodAddAdds the ListBox at the specified index location.
Public methodClearRemoves all controls from the collection.
Public methodContainsDetermines whether the specified index is located within the collection.
Public methodCopyToCopies the entire collection into an existing array at a specified location within the array.
Public methodEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetEnumeratorReturns an enumerator to use to iterate through the selected indexes collection.
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodIndexOfReturns the index within the ListBox.SelectedIndexCollection of the specified index from the ListBox.ObjectCollection of the ListBox.
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodRemoveRemoves the specified control from the collection.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top
  NameDescription
Public Extension MethodAsParallelEnables parallelization of a query. (Defined by ParallelEnumerable.)
Public Extension MethodAsQueryableConverts an IEnumerable to an IQueryable. (Defined by Queryable.)
Public Extension MethodCast<TResult>Casts the elements of an IEnumerable to the specified type. (Defined by Enumerable.)
Public Extension MethodOfType<TResult>Filters the elements of an IEnumerable based on a specified type. (Defined by Enumerable.)
Top
  NameDescription
Explicit interface implemetationPrivate propertyICollection.IsSynchronizedInfrastructure. For a description of this member, see ICollection.IsSynchronized.
Explicit interface implemetationPrivate propertyICollection.SyncRootInfrastructure. For a description of this member, see ICollection.SyncRoot.
Explicit interface implemetationPrivate methodIList.AddInfrastructure. For a description of this member, see IList.Add.
Explicit interface implemetationPrivate methodIList.ClearInfrastructure. For a description of this member, see IList.Clear.
Explicit interface implemetationPrivate methodIList.ContainsInfrastructure. For a description of this member, see IList.Contains.
Explicit interface implemetationPrivate methodIList.IndexOfInfrastructure. For a description of this member, see IList.IndexOf.
Explicit interface implemetationPrivate methodIList.InsertInfrastructure. For a description of this member, see IList.Insert.
Explicit interface implemetationPrivate propertyIList.IsFixedSizeInfrastructure. For a description of this member, see IList.IsFixedSize.
Explicit interface implemetationPrivate propertyIList.ItemInfrastructure. For a description of this member, see IList.Item.
Explicit interface implemetationPrivate methodIList.RemoveInfrastructure. For a description of this member, see IList.Remove.
Explicit interface implemetationPrivate methodIList.RemoveAtInfrastructure. For a description of this member, see IList.RemoveAt.
Top

The ListBox.SelectedIndexCollection class stores the indexes to the selected items in the ListBox. The indexes stored in the ListBox.SelectedIndexCollection are index positions within the ListBox.ObjectCollection class. The ListBox.ObjectCollection class stores all items displayed in the ListBox.

The following table is an example of how the ListBox.ObjectCollection stores the items of the ListBox as well as their selection states within an example ListBox.

Index

Item

Selection state within the ListBox

0

object1

Unselected

1

object2

Selected

2

object3

Unselected

3

object4

Selected

4

object5

Selected

Based on the ListBox.ObjectCollection example in the previous table, the following table demonstrates how the ListBox.SelectedIndexCollection would appear.

Index

Index of selected item in ObjectCollection

0

1

1

3

2

4

You can use the properties and methods of this class to perform a variety of tasks with the collection. The Contains method enables you to determine whether an index position from the ListBox.ObjectCollection class is a member of the selected indexes stored in the ListBox.SelectedIndexCollection. Once you know that the item is located within the collection, you can use the IndexOf method to determine where a specific index position within the ListBox.ObjectCollection for the ListBox is stored.

The following 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.
      do
      {
         // 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])
                  return;
            }
            // Select the item in the ListBox once it is found.
            listBox1.SetSelected(x,true);
         }

      }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.
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.