ListBox.SelectedIndexCollection 類別

定義

表示包含在 ListBox 中所選取項目索引的集合。Represents the collection containing the indexes to the selected items in a 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 的專案中搜尋搜尋文字的所有實例。The following example demonstrates how to use the FindString method to search for all instances of the search text in the items of the ListBox. 此範例使用 FindString 方法的版本,可讓您指定起始搜尋索引,以便持續搜尋 ListBox 中的所有專案。The example uses the version of the FindString method that enables you to specify a starting search index from which to do a continual search of all items in the ListBox. 此範例也會示範如何判斷 FindString 方法在到達專案清單底部之後從清單頂端開始搜尋,以防止遞迴搜尋。The example also demonstrates how to determine when the FindString method begins searching from the top of the list after it reaches the bottom of the list of items to prevent a recursive search. 一旦在 ListBox 中找到專案,就會使用 SetSelected 方法來選取。Once items are found in the ListBox, they are selected using the SetSelected method.

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

備註

@No__t-0 類別會將索引儲存至 ListBox 中選取的專案。The ListBox.SelectedIndexCollection class stores the indexes to the selected items in the ListBox. 儲存在 ListBox.SelectedIndexCollection 中的索引是 @no__t 1 類別內的索引位置。The indexes stored in the ListBox.SelectedIndexCollection are index positions within the ListBox.ObjectCollection class. @No__t-0 類別會儲存 ListBox 中顯示的所有專案。The ListBox.ObjectCollection class stores all items displayed in the ListBox.

下表是 ListBox.ObjectCollection 在範例 ListBox 中儲存 ListBox 之專案及其選取狀態的範例。The following table is an example of how the ListBox.ObjectCollection stores the items of the ListBox as well as their selection states within an example ListBox.

索引Index 項目Item 清單方塊內的選取狀態Selection state within the ListBox
00 object1object1 未選取Unselected
11 object2object2 已選取Selected
22 object3object3 未選取Unselected
33 object4object4 已選取Selected
44 object5object5 已選取Selected

根據上表中的 @no__t 0 範例,下表將示範如何顯示 ListBox.SelectedIndexCollectionBased on the ListBox.ObjectCollection example in the previous table, the following table demonstrates how the ListBox.SelectedIndexCollection would appear.

索引Index ObjectCollection 中選取專案的索引Index of selected item in ObjectCollection
00 11
11 33
22 44

您可以使用這個類別的屬性和方法,利用集合來執行各種工作。You can use the properties and methods of this class to perform a variety of tasks with the collection. @No__t-0 方法可讓您判斷來自 ListBox.ObjectCollection 類別的索引位置是否為儲存在 ListBox.SelectedIndexCollection 中所選取索引的成員。The Contains method enables you to determine whether an index position from the ListBox.ObjectCollection class is a member of the selected indexes stored in the ListBox.SelectedIndexCollection. 一旦您知道該專案位於集合中,您就可以使用 IndexOf 方法來判斷如何儲存 ListBoxListBox.ObjectCollection 內的特定索引位置。Once you know that the item is located within the collection, you can use the IndexOf method to determine where a specific index position within the ListBox.ObjectCollection for the ListBox is stored.

建構函式

ListBox.SelectedIndexCollection(ListBox)

初始化 ListBox.SelectedIndexCollection 類別的新執行個體。Initializes a new instance of the ListBox.SelectedIndexCollection class.

屬性

Count

取得集合中的項目數目。Gets the number of items in the collection.

IsReadOnly

取得值,表示集合是否為唯讀。Gets a value indicating whether the collection is read-only.

Item[Int32]

取得這個集合中指定索引處的索引值。Gets the index value at the specified index within this collection.

方法

Add(Int32)

在指定索引位置上加入 ListBoxAdds the ListBox at the specified index location.

Clear()

移除集合中所有的控制項。Removes all controls from the collection.

Contains(Int32)

判斷指定的索引是否位於集合中。Determines whether the specified index is located within the collection.

CopyTo(Array, Int32)

將整個集合複製到現有陣列中的指定位置。Copies the entire collection into an existing array at a specified location within the array.

Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
GetEnumerator()

傳回列舉值,其可在選取索引集合中重複。Returns an enumerator to use to iterate through the selected indexes collection.

GetHashCode()

作為預設雜湊函數。Serves as the default hash function.

(繼承來源 Object)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
IndexOf(Int32)

ListBox.SelectedIndexCollectionListBox.ObjectCollection 傳回指定索引在 ListBox 中的索引。Returns the index within the ListBox.SelectedIndexCollection of the specified index from the ListBox.ObjectCollection of the ListBox.

MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
Remove(Int32)

從集合中移除指定的控制項。Removes the specified control from the collection.

ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)

明確介面實作

ICollection.IsSynchronized

如需這個成員的說明,請參閱 IsSynchronizedFor a description of this member, see IsSynchronized.

ICollection.SyncRoot

如需這個成員的說明,請參閱 SyncRootFor a description of this member, see SyncRoot.

IList.Add(Object)

如需這個成員的說明,請參閱 Add(Object)For a description of this member, see Add(Object).

IList.Clear()

如需這個成員的說明,請參閱 Clear()For a description of this member, see Clear().

IList.Contains(Object)

如需這個成員的說明,請參閱 Contains(Object)For a description of this member, see Contains(Object).

IList.IndexOf(Object)

如需這個成員的說明,請參閱 IndexOf(Object)For a description of this member, see IndexOf(Object).

IList.Insert(Int32, Object)

如需這個成員的說明,請參閱 Insert(Int32, Object)For a description of this member, see Insert(Int32, Object).

IList.IsFixedSize

如需這個成員的說明,請參閱 IsFixedSizeFor a description of this member, see IsFixedSize.

IList.Item[Int32]

如需這個成員的說明,請參閱 Item[Int32]For a description of this member, see Item[Int32].

IList.Remove(Object)

如需這個成員的說明,請參閱 Remove(Object)For a description of this member, see Remove(Object).

IList.RemoveAt(Int32)

如需這個成員的說明,請參閱 RemoveAt(Int32)For a description of this member, see RemoveAt(Int32).

擴充方法

Cast<TResult>(IEnumerable)

IEnumerable 的項目轉換成指定的型別。Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable)

根據指定的型別來篩選 IEnumerable 的項目。Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable)

啟用查詢的平行化作業。Enables parallelization of a query.

AsQueryable(IEnumerable)

IEnumerable 轉換成 IQueryableConverts an IEnumerable to an IQueryable.

適用於