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 方法的版本,通过此方法,您可以指定起始搜索索引,以便对 @no__t 中的所有项进行持续搜索。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. 该示例还演示了如何确定 @no__t 0 方法在到达项列表的底部后从列表顶部开始搜索,以防止递归搜索。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. 在 @no__t 中找到项后,将使用 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 中存储 @no__t 的项以及这些项的选择状态。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.

IndexIndex 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.SelectedIndexCollection 的显示方式。Based on the ListBox.ObjectCollection example in the previous table, the following table demonstrates how the ListBox.SelectedIndexCollection would appear.

IndexIndex 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 方法,您可以确定 @no__t 类中的索引位置是否为存储在 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 方法确定 ListBox 的 @no__t 中特定索引位置的存储位置。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.SelectedIndexCollection 内的索引,该指定索引来自 ListBox.ObjectCollectionListBoxReturns 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()

返回一个表示当前对象的 string。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.

适用于