ListBox.SelectedIndices 속성

정의

ListBox에서 현재 선택되어 있는 모든 항목의 인덱스(0부터 시작)가 들어 있는 컬렉션을 가져옵니다.

public:
 property System::Windows::Forms::ListBox::SelectedIndexCollection ^ SelectedIndices { System::Windows::Forms::ListBox::SelectedIndexCollection ^ get(); };
[System.ComponentModel.Browsable(false)]
public System.Windows.Forms.ListBox.SelectedIndexCollection SelectedIndices { get; }
[<System.ComponentModel.Browsable(false)>]
member this.SelectedIndices : System.Windows.Forms.ListBox.SelectedIndexCollection
Public ReadOnly Property SelectedIndices As ListBox.SelectedIndexCollection

속성 값

ListBox.SelectedIndexCollection

현재 컨트롤에 선택되어 있는 항목의 인덱스가 들어 있는 ListBox.SelectedIndexCollection입니다. 현재 선택된 항목이 없으면 비어 있는 ListBox.SelectedIndexCollection이 반환됩니다.

특성

예제

다음 코드 예제에서는 메서드를 사용하여 FindString 항목 ListBox에서 검색 텍스트의 모든 인스턴스를 검색하는 방법을 보여 줍니다. 이 예제에서는 모든 항목을 ListBox지속적으로 검색할 시작 검색 인덱스를 지정할 수 있는 메서드 버전을 FindString 사용합니다. 이 예제에서는 재귀 검색을 방지하기 위해 항목 목록의 맨 아래에 도달한 후 메서드가 목록 맨 위에서 검색을 시작하는 시점 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의 경우 이 속성은 인덱스가 포함된 컬렉션을 해당 항목에서 선택한 모든 항목에 ListBox반환합니다. 단일 선택 ListBox영역의 경우 이 속성은 에서 유일하게 선택한 항목 ListBox의 인덱스가 포함된 단일 요소를 포함하는 컬렉션을 반환합니다. 컬렉션의 항목을 조작하는 방법에 대한 자세한 내용은 다음을 참조하세요 ListBox.SelectedIndexCollection.

클래스는 ListBox 선택한 항목을 참조하는 여러 가지 방법을 제공합니다. 속성을 사용하여 SelectedIndices 단일 선택 ListBox영역에서 현재 선택한 항목의 인덱스 위치를 가져오는 대신 이 속성을 사용할 SelectedIndex 수 있습니다. 항목의 인덱스 위치 대신 현재 선택된 ListBox항목을 가져오려면 이 속성을 사용합니다 SelectedItem . 또한 선택한 모든 항목을 다중 선택 ListBox항목으로 가져오려는 경우 이 속성을 사용할 SelectedItems 수 있습니다.

적용 대상

추가 정보