Export (0) Print
Expand All

ListView.SelectedIndexChanged Event

Occurs when the SelectedIndices collection changes.

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

public event EventHandler SelectedIndexChanged

The SelectedIndices collection changes whenever the Selected property of a ListViewItem changes. The property change can occur programmatically or when the user selects an item or clears the selection of an item. When the user selects an item without pressing CTRL to perform a multiple selection, the control first clears the previous selection. In this case, this event occurs one time for each item that was previously selected and one time for the newly selected item.

To determine which items are selected in the ListView control, use the SelectedItems property to access the ListView.SelectedListViewItemCollection. You can create an event handler for this event to perform tasks whenever there is a change to the selected items in the ListView control.

For more information about handling events, see Consuming Events.

The following code example demonstrates how to use the SelectedItems property, SelectedIndexChanged event, HeaderStyle members, and the ListView.SelectedListViewItemCollection 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 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 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;

		this.ListView1.Columns.Add(columnHeader1);
		this.ListView1.Columns.Add(columnHeader2);

		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]);
			listItem.SubItems.Add(foodPrice[count]);
			ListView1.Items.Add(listItem);
		}
		this.Controls.Add(ListView1);
	}
	


...


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

		ListView.SelectedListViewItemCollection breakfast = 
			this.ListView1.SelectedItems;
		
		double price = 0.0;
		foreach ( ListViewItem item in breakfast )
		{
			price += Double.Parse(item.SubItems[1].Text);
		}

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



ListView.SelectedItems, ListView.SelectedIndexChanged event, 


.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.

Community Additions

ADD
Show:
© 2014 Microsoft