ListBox.SelectedIndexChanged Event
Occurs when the SelectedIndex property or the SelectedIndices collection has changed.
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
You can create an event handler for this event to determine when the selected index in the ListBox has been changed. This can be useful when you need to display information in other controls based on the current selection in the ListBox. You can use the event handler for this event to load the information in the other controls.
If the SelectionMode property is set to SelectionMode.MultiSimple or SelectionMode.MultiExtended, any change to the SelectedIndices collection, including removing an item from the selection, will raise this event.
For more information about handling events, see Consuming Events.
The following code example demonstrates how to use the SelectedIndexChanged event to search for and select an item in a different ListBox control. The example uses the SelectedIndexChanged event to determine when the selected item in the ListBox is changed. The example code then reads the text of the item using the SelectedItem property and calls the FindString method on a different ListBox using the text returned by SelectedItem in the first ListBox. If an item is found in the other ListBox, the item is selected. This example requires that two ListBox controls, named listBox1 and listBox2, have been added to a form and that both ListBox controls contain items that are identical. The example also requires that the event-handling method defined in the example is connected to the SelectedIndexChanged event of listBox1.
private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e) { // Get the currently selected item in the ListBox. string curItem = listBox1.SelectedItem.ToString(); // Find the string in ListBox2. int index = listBox2.FindString(curItem); // If the item was not found in ListBox 2 display a message box, otherwise select it in ListBox2. if(index == -1) MessageBox.Show("Item is not available in ListBox2"); else listBox2.SetSelected(index,true); }
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.
This example shows another way that this event is fired and give you a false trigger!
When you have a struct and you add items to the listBox based on this struct, if you change the contents of the item the SelectedIndexChanged will be call:
private struct strPair
{
public int UID;
public string Contents;
public override string ToString()
{
return Contents;
}
}
private void Form1_Load(object sender, EventArgs e)
{
private strPair p = new strPair();
p.UID = 3;
p.Contents = "something1";
listBox1.Items.Add(p);
listBox1.selectedIndex = 0;
}
now, if you write this:
private void ChangeItem()
{
strPair p = new strPair();
p = (strPair)listBox1.SelectedItem;
p.Contents = "something2";
listBox1.Items[0] = p;
You will be noticed that the SelectedIndexChanged event was fired at the line: listBox1.Items[0] = p;
This is not cool and you need to write extra code to avoid execute the code inside the event in this situation.
To do this, you need to create a global bool flag and set it to true before change the item, and set it to false after that.
And inside your SelectedIndexChanged event, you need to check this flag first and if it is true you do a return;
- 9/3/2010
- †GnR† Slash