ListBox.SelectedIndexCollection クラス

定義

ListBoxで選択した項目のインデックスを含むコレクションを表します。

public: ref class ListBox::SelectedIndexCollection : System::Collections::IList
public class ListBox.SelectedIndexCollection : System.Collections.IList
type ListBox.SelectedIndexCollection = class
    interface IList
    interface ICollection
    interface IEnumerable
Public Class ListBox.SelectedIndexCollection
Implements IList
継承
ListBox.SelectedIndexCollection
実装

次の例では、 FindString メソッドを使用して、 ListBoxの項目内の検索テキストのすべてのインスタンスを検索する方法を示します。 この例では、 FindString メソッドのバージョンを使用して、 ListBox内のすべての項目の継続的な検索を行う開始検索インデックスを指定できます。 この例では、 FindString メソッドが項目の一覧の一番下に到達した後、リストの先頭から検索を開始して再帰的な検索を回避する方法も示します。 ListBoxで項目が見つかると、SetSelectedメソッドを使用して項目が選択されます。

private:
   void FindAllOfMyString( String^ searchString )
   {
      // Set the SelectionMode property of the ListBox to select multiple items.
      listBox1->SelectionMode = SelectionMode::MultiExtended;

      // Set our intial index variable to -1.
      int x = -1;

      // If the search string is empty exit.
      if ( searchString->Length != 0 )
      {
         // Loop through and find each item that matches the search string.
         do
         {
            // Retrieve the item based on the previous index found. Starts with -1 which searches start.
            x = listBox1->FindString( searchString, x );

            // If no item is found that matches exit.
            if ( x != -1 )
            {
               // Since the FindString loops infinitely, determine if we found first item again and exit.
               if ( listBox1->SelectedIndices->Count > 0 )
               {
                  if ( x == listBox1->SelectedIndices[ 0 ] )
                                    return;
               }

               // Select the item in the ListBox once it is found.
               listBox1->SetSelected( x, true );
            }
         }
         while ( x != -1 );
      }
   }
private void FindAllOfMyString(string searchString)
{
   // Set the SelectionMode property of the ListBox to select multiple items.
   listBox1.SelectionMode = SelectionMode.MultiExtended;
   
   // Set our intial index variable to -1.
   int x =-1;
   // If the search string is empty exit.
   if (searchString.Length != 0)
   {
      // Loop through and find each item that matches the search string.
      do
      {
         // Retrieve the item based on the previous index found. Starts with -1 which searches start.
         x = listBox1.FindString(searchString, x);
         // If no item is found that matches exit.
         if (x != -1)
         {
            // Since the FindString loops infinitely, determine if we found first item again and exit.
            if (listBox1.SelectedIndices.Count > 0)
            {
               if(x == listBox1.SelectedIndices[0])
                  return;
            }
            // Select the item in the ListBox once it is found.
            listBox1.SetSelected(x,true);
         }
      }while(x != -1);
   }
}
Private Sub FindAllOfMyString(ByVal searchString As String)
   ' Set the SelectionMode property of the ListBox to select multiple items.
   listBox1.SelectionMode = SelectionMode.MultiExtended

   ' Set our intial index variable to -1.
   Dim x As Integer = -1
   ' If the search string is empty exit.
   If searchString.Length <> 0 Then
      ' Loop through and find each item that matches the search string.
      Do
         ' Retrieve the item based on the previous index found. Starts with -1 which searches start.
         x = listBox1.FindString(searchString, x)
         ' If no item is found that matches exit.
         If x <> -1 Then
            ' Since the FindString loops infinitely, determine if we found first item again and exit.
            If ListBox1.SelectedIndices.Count > 0 Then
               If x = ListBox1.SelectedIndices(0) Then
                  Return
               End If
            End If
            ' Select the item in the ListBox once it is found.
            ListBox1.SetSelected(x, True)
         End If
      Loop While x <> -1
   End If
End Sub

注釈

ListBox.SelectedIndexCollection クラスは、ListBox内の選択した項目へのインデックスを格納します。 ListBox.SelectedIndexCollectionに格納されているインデックスは、ListBox.ObjectCollection クラス内のインデックス位置です。 ListBox.ObjectCollection クラスは、ListBoxに表示されるすべての項目を格納します。

次の表は、 ListBox.ObjectCollectionListBox の項目とその選択状態を ListBox例内に格納する方法の例です。

Index 品目 ListBox 内の選択状態
0 object1 未選択
1 object2 選択済み
2 object3 未選択
3 object4 選択済み
4 object5 選択済み

次の表は、前の表の ListBox.ObjectCollection 例に基づいて、 ListBox.SelectedIndexCollection がどのように表示されるかを示しています。

Index ObjectCollection で選択した項目のインデックス
0 1
1 3
2 4

このクラスのプロパティとメソッドを使用して、コレクションでさまざまなタスクを実行できます。 Contains メソッドを使用すると、ListBox.ObjectCollection クラスからのインデックス位置が、ListBox.SelectedIndexCollectionに格納されている選択したインデックスのメンバーであるかどうかを判断できます。 アイテムがコレクション内にあることがわかっている場合は、IndexOf メソッドを使用して、ListBoxListBox.ObjectCollection内の特定のインデックス位置を決定できます。

コンストラクター

名前 説明
ListBox.SelectedIndexCollection(ListBox)

ListBox.SelectedIndexCollection クラスの新しいインスタンスを初期化します。

プロパティ

名前 説明
Count

コレクション内のアイテムの数を取得します。

IsReadOnly

コレクションが読み取り専用かどうかを示す値を取得します。

Item[Int32]

このコレクション内の指定したインデックス位置にあるインデックス値を取得します。

メソッド

名前 説明
Add(Int32)

指定したインデックス位置に ListBox を追加します。

Clear()

コレクションからすべてのコントロールを削除します。

Contains(Int32)

指定したインデックスがコレクション内にあるかどうかを判断します。

CopyTo(Array, Int32)

コレクション全体を、配列内の指定した位置にある既存の配列にコピーします。

Equals(Object)

指定したオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
GetEnumerator()

選択したインデックス コレクションを反復処理するために使用する列挙子を返します。

GetHashCode()

既定のハッシュ関数として機能します。

(継承元 Object)
GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
IndexOf(Int32)

ListBoxListBox.ObjectCollectionから、指定したインデックスのListBox.SelectedIndexCollection内のインデックスを返します。

MemberwiseClone()

現在の Objectの簡易コピーを作成します。

(継承元 Object)
Remove(Int32)

指定したコントロールをコレクションから削除します。

ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)

明示的なインターフェイスの実装

名前 説明
ICollection.IsSynchronized

このメンバーの説明については、 IsSynchronizedを参照してください。

ICollection.SyncRoot

このメンバーの説明については、 SyncRootを参照してください。

IList.Add(Object)

このメンバーの説明については、 Add(Object)を参照してください。

IList.Clear()

このメンバーの説明については、 Clear()を参照してください。

IList.Contains(Object)

このメンバーの説明については、 Contains(Object)を参照してください。

IList.IndexOf(Object)

このメンバーの説明については、 IndexOf(Object)を参照してください。

IList.Insert(Int32, Object)

このメンバーの説明については、 Insert(Int32, Object)を参照してください。

IList.IsFixedSize

このメンバーの説明については、 IsFixedSizeを参照してください。

IList.Item[Int32]

このメンバーの説明については、 Item[Int32]を参照してください。

IList.Remove(Object)

このメンバーの説明については、 Remove(Object)を参照してください。

IList.RemoveAt(Int32)

このメンバーの説明については、 RemoveAt(Int32)を参照してください。

拡張メソッド

名前 説明
AsParallel(IEnumerable)

クエリの並列化を有効にします。

AsQueryable(IEnumerable)

IEnumerableIQueryableに変換します。

Cast<TResult>(IEnumerable)

IEnumerable の要素を指定した型にキャストします。

OfType<TResult>(IEnumerable)

指定した型に基づいて、IEnumerable の要素をフィルター処理します。

適用対象