クリックして評価とフィードバックをお寄せください
MSDN
MSDN ライブラリ
.NET 開発
以前のバージョン
.NET Framework SDK 2.0
System.Windows.Forms
すべて縮小/すべて展開 すべて縮小
このページは次のバージョンについて記述しています。
Microsoft Visual Studio 2005/.NET Framework 2.0

その他のバージョンについては、以下の情報を参照してください。
.NET Framework クラス ライブラリ
ListBox.SelectedIndexCollection クラス

ListBox 内で選択されている項目のインデックスを格納するコレクションを表します。

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

Visual Basic (宣言)
Public Class SelectedIndexCollection
    Implements IList, ICollection, IEnumerable
Visual Basic (使用法)
Dim instance As SelectedIndexCollection
C#
public class SelectedIndexCollection : IList, ICollection, IEnumerable
C++
public ref class SelectedIndexCollection : IList, ICollection, IEnumerable
J#
public class SelectedIndexCollection implements IList, ICollection, 
    IEnumerable
JScript
public class SelectedIndexCollection implements IList, ICollection, 
    IEnumerable

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.ObjectCollection クラス内のインデックス位置が、選択されたインデックスを格納する ListBox.SelectedIndexCollection のメンバかどうかを確認できます。このコレクションにインデックス位置があることがわかれば、IndexOf メソッドを使用して、ListBoxListBox.ObjectCollection 内の特定のインデックス位置が、このコレクション内のどこにあるかを判断できます。

FindString メソッドを使用し、ListBox の項目内から検索テキストのすべてのインスタンスを探し出す方法を次の例に示します。この例では、ListBox 内のすべての項目の連続検索を実行する際の開始検索インデックスを指定できるバージョンの FindString メソッドを使用しています。この例では、再帰的な検索を防ぐために、項目のリストの最後に到達した後、リストの先頭から FindString メソッドが検索を開始するときを判定する方法についても示します。ListBox に項目が見つかったら、SetSelected メソッドを使用して選択されます。

Visual Basic
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
C#
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);
   }
}
C++
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 );
      }
   }
J#
    private void FindAllOfMyString(String searchString)
    {
        // Set the SelectionMode property of the ListBox to 
        // select multiple items.
        listBox1.set_SelectionMode(SelectionMode.MultiExtended);
        // Set our intial index variable to -1.
        int x = -1;

        // If the search string is empty exit.
        if (searchString.get_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.get_SelectedIndices().get_Count() > 0) {
                        if (x == listBox1.get_SelectedIndices().get_Item(0)) {
                            return;
                        }
                    }
                    // Select the item in the ListBox once it is found.
                    listBox1.SetSelected(x, true);
                }
            } while (x != -1);
        }
    } //FindAllOfMyString 
} //Form1
System.Object
  System.Windows.Forms.ListBox.SelectedIndexCollection
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバの場合は、スレッド セーフであるとは限りません。

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, 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
コミュニティ コンテンツ   コミュニティ コンテンツとは
新しいコンテンツの追加 RSS  注釈
Processing
© 2009 Microsoft Corporation. All rights reserved. 使用条件 | 商標 | プライバシー
Page view tracker