ListBox.SelectedIndexCollection.Count プロパティ

定義

コレクション内の項目の数を取得します。

public:
 property int Count { int get(); };
[System.ComponentModel.Browsable(false)]
public int Count { get; }
[<System.ComponentModel.Browsable(false)>]
member this.Count : int
Public ReadOnly Property Count As Integer

プロパティ値

Int32

コレクション内の項目数。

実装

属性

次の例では、メソッドを 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決定できます。 その後、コレクションの値をループ処理するときにこの値を使用できます。ループを実行するには、いくつかのイテレーションを指定する必要があります。 このプロパティのSelectionModeListBoxプロパティが設定SelectionMode.MultiSimpleされている場合をSelectionMode.MultiExtended除き、このプロパティは、選択した項目があるかどうかに応じて、常に 0 (0) または 1 (1) の値を返します。

適用対象