ListView.SelectedIndexCollection Class
Represents the collection that contains the indexes to the selected items in a ListView control.
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
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.
' This method adds two columns to the ListView, setting the Text ' and TextAlign, and Width properties of each ColumnHeader. The ' HeaderStyle property is set to NonClickable since the ColumnClick ' event is not handled. Finally the method adds ListViewItems and ' SubItems to each column. Private Sub InitializeListView() Me.ListView1 = New System.Windows.Forms.ListView Me.ListView1.BackColor = System.Drawing.SystemColors.Control Me.ListView1.Dock = System.Windows.Forms.DockStyle.Top Me.ListView1.Location = New System.Drawing.Point(0, 0) Me.ListView1.Name = "ListView1" Me.ListView1.Size = New System.Drawing.Size(292, 130) Me.ListView1.TabIndex = 0 Me.ListView1.View = System.Windows.Forms.View.Details Me.ListView1.MultiSelect = True Me.ListView1.HideSelection = False ListView1.HeaderStyle = ColumnHeaderStyle.Nonclickable Dim columnHeader1 As New ColumnHeader With columnHeader1 .Text = "Breakfast Item" .TextAlign = HorizontalAlignment.Left .Width = 146 End With Dim columnHeader2 As New ColumnHeader With columnHeader2 .Text = "Price Each" .TextAlign = HorizontalAlignment.Center .Width = 142 End With Me.ListView1.Columns.Add(columnHeader1) Me.ListView1.Columns.Add(columnHeader2) Dim foodList() As String = New String() {"Juice", "Coffee", _ "Cereal & Milk", "Fruit Plate", "Toast & Jelly", _ "Bagel & Cream Cheese"} Dim foodPrice() As String = New String() {"1.09", "1.09", _ "2.19", "2.49", "1.49", "1.49"} Dim count As Integer For count = 0 To foodList.Length - 1 Dim listItem As New ListViewItem(foodList(count)) listItem.SubItems.Add(foodPrice(count)) ListView1.Items.Add(listItem) Next Me.Controls.Add(Me.ListView1) End Sub ... ' Uses the SelectedIndices property to retrieve and tally the price of ' the selected menu items. Private Sub ListView1_SelectedIndexChanged_UsingIndices _ (ByVal sender As Object, ByVal e As System.EventArgs) _ Handles ListView1.SelectedIndexChanged Dim indexes As ListView.SelectedIndexCollection = _ Me.ListView1.SelectedIndices Dim index As Integer Dim price As Double = 0.0 For Each index In indexes price += Double.Parse(Me.ListView1.Items(index).SubItems(1).Text) Next ' Output the price to TextBox1. TextBox1.Text = CType(price, String) End Sub
ListView.SelectIndices, SelectedIndexCollection, ListView.SelectedIndexChanged event,
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, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC
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.