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と、その選択状態を 例内に格納する方法ListBox.ObjectCollectionの例ListBoxです。

インデックス 項目 ListBox 内の選択状態
0 object1 未選択
1 object2 オン
2 object3 未選択
3 object4 オン
4 object5 オン

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

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

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

コンストラクター

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)

ListBox.SelectedIndexCollectionListBox.ObjectCollection 内で指定されたインデックスについて、ListBox 内におけるインデックスを返します。

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)」をご覧ください。

拡張メソッド

Cast<TResult>(IEnumerable)

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

OfType<TResult>(IEnumerable)

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

AsParallel(IEnumerable)

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

AsQueryable(IEnumerable)

IEnumerableIQueryable に変換します。

適用対象