SelectedIndices, propriété
Cet article a fait l'objet d'une traduction automatique. Déplacez votre pointeur sur les phrases de l'article pour voir la version originale de ce texte. Informations supplémentaires.

ListView.SelectedIndices, propriété

Obtient les index des éléments sélectionnés dans le contrôle.

Espace de noms :  System.Windows.Forms
Assembly :  System.Windows.Forms (dans System.Windows.Forms.dll)

public ListView.SelectedIndexCollection SelectedIndices { get; }

Valeur de propriété

Type : System.Windows.Forms.ListView.SelectedIndexCollection
ListView.SelectedIndexCollection qui contient les index des éléments sélectionnés. Si aucun élément n'est sélectionné, un ListView.SelectedIndexCollection vide est retourné.

The SelectedIndices property will not contain any indexes if the property is accessed before the ListView handle is created, which typically occurs when the ListView is initially loaded for display in the form. You can check to see if the handle is created with the IsHandleCreated property. When the MultiSelect property is set to true, this property returns a collection containing the indexes of all items that are selected in the ListView. For a single-selection ListView, this property returns a collection containing a single element containing the index of the only selected item in the ListView. For more information on the tasks that can be performed with the items in the collection, see ListView.SelectedIndexCollection.

If you want to obtain a collection of the items that are selected in the ListView control, instead of the index positions of the items that are selected, use the SelectedItems property.

The following code example demonstrates using 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 method. This example requires 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 void InitializeListView()
		this.ListView1 = new System.Windows.Forms.ListView();
		this.ListView1.BackColor = System.Drawing.SystemColors.Control;
		this.ListView1.Dock = System.Windows.Forms.DockStyle.Top;
		this.ListView1.Location = new System.Drawing.Point(0, 0);
		this.ListView1.Name = "ListView1";
		this.ListView1.Size = new System.Drawing.Size(292, 130);
		this.ListView1.TabIndex = 0;
		this.ListView1.View = System.Windows.Forms.View.Details;
		this.ListView1.MultiSelect = true;
		this.ListView1.HideSelection = false;
		this.ListView1.HeaderStyle = ColumnHeaderStyle.Nonclickable;
		ColumnHeader columnHeader1 = new ColumnHeader();
		columnHeader1.Text = "Breakfast Item";
		columnHeader1.TextAlign = HorizontalAlignment.Left;
		columnHeader1.Width = 146;

	 	ColumnHeader columnHeader2 = new ColumnHeader();
		columnHeader2.Text = "Price Each";
		columnHeader2.TextAlign = HorizontalAlignment.Center;
		columnHeader2.Width = 142;


		string[] foodList = new string[]{"Juice", "Coffee", 
			"Cereal & Milk", "Fruit Plate", "Toast & Jelly", 
			"Bagel & Cream Cheese"};
		string[] foodPrice = new string[]{"1.09", "1.09", "2.19", 
			"2.49", "1.49", "1.49"};
		for(int count=0; count < foodList.Length; count++)
			ListViewItem listItem = new ListViewItem(foodList[count]);


	// Uses the SelectedIndices property to retrieve and tally the  
	// price of the selected menu items.
	private void ListView1_SelectedIndexChanged_UsingIndices(
		object sender, System.EventArgs e)

		ListView.SelectedIndexCollection indexes = 
		double price = 0.0;
		foreach ( int index in indexes )
			price += Double.Parse(

		// Output the price to TextBox1.
		TextBox1.Text =  price.ToString();

ListView.SelectIndices, SelectedIndexCollection, ListView.SelectedIndexChanged event, 

.NET Framework

Pris en charge dans : 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Pris en charge dans : 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (rôle principal du serveur non pris en charge), Windows Server 2008 R2 (rôle principal du serveur pris en charge avec SP1 ou version ultérieure ; Itanium non pris en charge)

.NET Framework ne prend pas en charge toutes les versions de chaque plateforme. Pour obtenir la liste des versions prises en charge, consultez Configuration requise du .NET Framework.

Ajouts de la communauté

© 2015 Microsoft