次の方法で共有


ListBox.SelectedIndexChanged イベント

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 プロパティを使用して項目のテキストを読み取り、最初の ListBoxSelectedItem で返されたテキストを使用して他の ListBox にある FindString メソッドを呼び出しています。他の ListBox で項目が見つかった場合、その項目は選択されています。この例では、listBox1 および listBox2 という名前の 2 つの ListBox コントロールがフォームに追加されており、この 2 つの ListBox コントロールの両方に同一の項目が格納されている必要があります。また、この例で定義されているイベント処理メソッドが、listBox1SelectedIndexChanged イベントに接続されている必要もあります。

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

参照

関連項目

ListBox クラス
ListBox メンバ
System.Windows.Forms 名前空間
ListBox.SelectedIndex プロパティ