다음을 통해 공유


ListBox.SelectedIndexCollection 클래스

ListBox에서 선택된 항목에 대해 인덱스가 들어 있는 컬렉션을 나타냅니다.

네임스페이스: System.Windows.Forms
어셈블리: System.Windows.Forms(system.windows.forms.dll)

구문

‘선언
Public Class SelectedIndexCollection
    Implements IList, ICollection, IEnumerable
‘사용 방법
Dim instance As SelectedIndexCollection
public class SelectedIndexCollection : IList, ICollection, IEnumerable
public ref class SelectedIndexCollection : IList, ICollection, IEnumerable
public class SelectedIndexCollection implements IList, ICollection, 
    IEnumerable
public class SelectedIndexCollection implements IList, ICollection, 
    IEnumerable

설명

ListBox.SelectedIndexCollection 클래스가 ListBox의 선택된 항목에 대한 인덱스를 저장합니다. ListBox.SelectedIndexCollection에 저장된 인덱스는 ListBox.ObjectCollection 클래스의 인덱스 위치입니다. ListBox.ObjectCollection 클래스는 ListBox에 표시된 모든 항목을 저장합니다.

다음 표는 ListBox에서 ListBox 예제의 선택 상태와 ListBox.ObjectCollection의 항목을 저장하는 방법을 보여 주는 예입니다.

인덱스

항목

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 메서드를 사용하여 해당 항목을 선택합니다.

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
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 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에서 지원

참고 항목

참조

ListBox.SelectedIndexCollection 멤버
System.Windows.Forms 네임스페이스