在
SelectedIndex 属性更改后发生。
命名空间:System.Windows.Forms
程序集:System.Windows.Forms(在 system.windows.forms.dll 中)
Public Event SelectedIndexChanged As EventHandler
Dim instance As ListBox
Dim handler As EventHandler
AddHandler instance.SelectedIndexChanged, handler
public event EventHandler SelectedIndexChanged
public:
event EventHandler^ SelectedIndexChanged {
void add (EventHandler^ value);
void remove (EventHandler^ value);
}
/** @event */
public void add_SelectedIndexChanged (EventHandler value)
/** @event */
public void remove_SelectedIndexChanged (EventHandler value)
JScript 支持使用事件,但不支持进行新的声明。
可以为该事件创建事件处理程序,以确定 ListBox 中选定的索引何时更改。这在需要根据 ListBox 中的当前选定内容显示其他控件中的信息时非常有用。可以使用该事件的事件处理程序来加载其他控件中的信息。
有关处理事件的更多信息,请参见 使用事件。
下面的代码示例演示如何使用 SelectedIndexChanged 事件搜索并选择不同 ListBox 控件中的项。该示例使用 SelectedIndexChanged 事件来确定 ListBox 中的选定项的更改时间。然后,该代码示例使用 SelectedItem 属性读取该项的文本并且使用第一个 ListBox 中的 SelectedItem 返回的文本调用其他 ListBox 上的 FindString 方法。如果在其他 ListBox 中找到项,则选择该项。该示例要求已将名为 listBox1 和 listBox2 的两个 ListBox 控件添加到窗体中,并且这两个 ListBox 控件包含相同的项。该示例还要求示例中定义的事件处理方法已连接到 listBox1 的 SelectedIndexChanged 事件。
Private Sub listBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles listBox1.SelectedIndexChanged
' Get the currently selected item in the ListBox.
Dim curItem As String = listBox1.SelectedItem.ToString()
' Find the string in ListBox2.
Dim index As Integer = listBox2.FindString(curItem)
' If the item was not found in ListBox 2 display a message box, otherwise select it in ListBox2.
If index = -1 Then
MessageBox.Show("Item is not available in ListBox2")
Else
listBox2.SetSelected(index, True)
End If
End Sub
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);
}
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 );
}
private void listBox1_SelectedIndexChanged(Object sender,
System.EventArgs e)
{
// Get the currently selected item in the ListBox.
String curItem = listBox1.get_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);
}
} //listBox1_SelectedIndexChanged
Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
.NET Framework
受以下版本支持:2.0、1.1、1.0
.NET Compact Framework
受以下版本支持:2.0、1.0