ListBox.SelectedIndexCollection ListBox.SelectedIndexCollection ListBox.SelectedIndexCollection ListBox.SelectedIndexCollection Class

定義

表示集合,其中含有 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.SelectedIndexCollectionListBox.SelectedIndexCollectionListBox.SelectedIndexCollectionListBox.SelectedIndexCollection
實作

範例

下列範例示範如何使用FindString方法來搜尋的搜尋文字的項目中的所有執行個體ListBoxThe 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方法,可讓您指定要從中執行中的所有項目持續搜尋開始的搜尋服務索引ListBoxThe 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

備註

ListBox.SelectedIndexCollection類別會儲存在選取的項目索引ListBoxThe ListBox.SelectedIndexCollection class stores the indexes to the selected items in the ListBox. 索引儲存在ListBox.SelectedIndexCollection內的索引位置ListBox.ObjectCollection類別。The indexes stored in the ListBox.SelectedIndexCollection are index positions within the ListBox.ObjectCollection class. ListBox.ObjectCollection類別會儲存所有項目顯示在ListBoxThe ListBox.ObjectCollection class stores all items displayed in the ListBox.

下表會如何的範例ListBox.ObjectCollection儲存的項目ListBox以及其在範例中的選取項目狀態ListBoxThe 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

根據ListBox.ObjectCollection上表中的範例下, 表會示範如何ListBox.SelectedIndexCollection會出現。Based 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. Contains方法可讓您判斷是否從索引位置ListBox.ObjectCollection類別是儲存在所選索引ListBox.SelectedIndexCollectionThe 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方法,以判斷其中內的特定索引位置ListBox.ObjectCollectionListBox儲存。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(ListBox) ListBox.SelectedIndexCollection(ListBox) ListBox.SelectedIndexCollection(ListBox)

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

屬性

Count Count Count Count

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

IsReadOnly IsReadOnly IsReadOnly IsReadOnly

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

Item[Int32] Item[Int32] Item[Int32] Item[Int32]

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

方法

Add(Int32) Add(Int32) Add(Int32) Add(Int32)

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

Clear() Clear() Clear() Clear()

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

Contains(Int32) Contains(Int32) Contains(Int32) Contains(Int32)

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

CopyTo(Array, Int32) CopyTo(Array, Int32) CopyTo(Array, Int32) CopyTo(Array, Int32)

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

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

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

(Inherited from Object)
GetEnumerator() GetEnumerator() GetEnumerator() GetEnumerator()

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

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

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

(Inherited from Object)
IndexOf(Int32) IndexOf(Int32) IndexOf(Int32) 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() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
Remove(Int32) Remove(Int32) Remove(Int32) Remove(Int32)

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

ToString() ToString() ToString() ToString()

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

(Inherited from Object)

明確介面實作

ICollection.IsSynchronized ICollection.IsSynchronized ICollection.IsSynchronized ICollection.IsSynchronized

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

ICollection.SyncRoot ICollection.SyncRoot ICollection.SyncRoot ICollection.SyncRoot

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

IList.Add(Object) IList.Add(Object) IList.Add(Object) IList.Add(Object)

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

IList.Clear() IList.Clear() IList.Clear() IList.Clear()

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

IList.Contains(Object) IList.Contains(Object) IList.Contains(Object) IList.Contains(Object)

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

IList.IndexOf(Object) IList.IndexOf(Object) IList.IndexOf(Object) IList.IndexOf(Object)

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

IList.Insert(Int32, Object) IList.Insert(Int32, Object) IList.Insert(Int32, Object) IList.Insert(Int32, Object)

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

IList.IsFixedSize IList.IsFixedSize IList.IsFixedSize IList.IsFixedSize

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

IList.Item[Int32] IList.Item[Int32] IList.Item[Int32] IList.Item[Int32]

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

IList.Remove(Object) IList.Remove(Object) IList.Remove(Object) IList.Remove(Object)

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

IList.RemoveAt(Int32) IList.RemoveAt(Int32) IList.RemoveAt(Int32) IList.RemoveAt(Int32)

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

Extension Methods

Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable)

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

OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable)

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

AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable)

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

AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable)

將轉換IEnumerableIQueryableConverts an IEnumerable to an IQueryable.

適用於