A ListView.SelectedIndexCollection stores the indexes to the selected items in a ListView control. The indexes stored in the ListView.SelectedIndexCollection are index positions within the ListView.ListViewItemCollection. The ListView.ListViewItemCollection stores all items displayed in the ListView control.

The following table shows an example of how the ListView.ListViewItemCollection stores the items of the ListView and shows their selection states in an example ListView.

Index

Item

Selection state in the ListView

0

Item1

Unselected

1

Item2

Selected

2

Item3

Unselected

3

Item4

Selected

4

Item5

Selected

Based on the ListView.ListViewItemCollection example in the previous table, the following table demonstrates how the ListView.SelectedIndexCollection would appear.

Index

Index of selected item in the ListViewItemCollection

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 ListView.ListViewItemCollection is one of the indexes stored in the ListView.SelectedIndexCollection. Once you know that the item is in the collection, you can use the IndexOf method to determine the position of the index in the ListView.SelectedIndexCollection.

The following code example demonstrates how to use the SelectedIndices, SelectedIndexChanged, and HeaderStyle members and the ListView.SelectedIndexCollection class. To run this example, paste the following code into a form that contains a ListView object named ListView1 and a TextBox named TextBox1. Call the InitializeListView method from the form's constructor or Load event handler. This example requires that the event handler is correctly associated with the SelectedIndexChanged event.