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.MultiSimpleSelectionMode.MultiExtendedいずれかに設定され (複数選択ListBox指定されている)、リスト内で複数の項目が選択されている場合、このプロパティは、選択されている任意の項目を返すことができます

複数選択ListBox 内で選択されているすべての項目を格納するコレクション取得するには、SelectedItems プロパティ使用しますListBox 内で現在選択されている項目のインデックス位置を取得するには、SelectedIndex プロパティ使用しますまた、SelectedIndices プロパティ使用して複数選択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 
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照


このページでは「.NET Framework クラス ライブラリ リファレンス」からListBox.SelectedItem プロパティを検索した結果を表示しています。
Weblioに収録されているすべての辞書からListBox.SelectedItem プロパティを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からListBox.SelectedItem プロパティ を検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

ListBox.SelectedItem プロパティのお隣キーワード
検索ランキング

   

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



ListBox.SelectedItem プロパティのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

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

©2025 GRAS Group, Inc.RSS