ListBox.SelectedItem プロパティとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > ListBox.SelectedItem プロパティの意味・解説 

ListBox.SelectedItem プロパティ

ListBox 内で現在選択されている項目を取得または設定します。

名前空間: System.Windows.Forms
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)
構文構文

<BindableAttribute(True)> _
Public Property SelectedItem As
 Object
Dim instance As ListBox
Dim value As Object

value = instance.SelectedItem

instance.SelectedItem = value
[BindableAttribute(true)] 
public Object SelectedItem { get; set;
 }
[BindableAttribute(true)] 
public:
property Object^ SelectedItem {
    Object^ get ();
    void set (Object^ value);
}
/** @property */
public Object get_SelectedItem ()

/** @property */
public void set_SelectedItem (Object value)
public function get SelectedItem
 () : Object

public function set SelectedItem
 (value : Object)

プロパティ値
コントロール内で現在選択されている項目を表すオブジェクト。

解説解説

標準の ListBox の場合は、このプロパティを使用して、ListBox 内で選択されている項目を確認できます。ListBox の SelectionMode プロパティが SelectionMode.MultiSimple か SelectionMode.MultiExtended のいずれかに設定され (複数選択の ListBox が指定されている)、リスト内で複数の項目が選択されている場合、このプロパティは、選択されている任意の項目を返すことができます。

複数選択の ListBox 内で選択されているすべての項目を格納するコレクションを取得するには、SelectedItems プロパティを使用します。ListBox 内で現在選択されている項目のインデックス位置を取得するには、SelectedIndex プロパティを使用します。また、SelectedIndices プロパティを使用して、複数選択の ListBox 内で選択されているすべてのインデックスを取得することもできます。

使用例使用例

SelectedIndexChanged イベントを使用して、他の ListBox コントロール内で項目を検索して選択する方法を次のコード例に示します。この例では、SelectedIndexChanged イベントを使用して、ListBox 内の選択項目がいつ変更されたかを判断しています。次に、このコード例は、SelectedItem プロパティを使用して項目のテキストを読み取り、最初の ListBox の SelectedItem で返されたテキストを使用して他の ListBox にある FindString メソッドを呼び出しています。他の ListBox で項目が見つかった場合、その項目は選択されています。この例では、listBox1 および listBox2 という名前の 2 つの ListBox コントロールがフォームに追加されており、この 2 つの 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 
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

ListBox.SelectedItem プロパティのお隣キーワード

   

英語⇒日本語
日本語⇒英語
   



ListBox.SelectedItem プロパティのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS