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.SelectedIndexCollection 内的索引,该指定索引来自 ListBox.ObjectCollectionListBoxReturns the index within the ListBox.SelectedIndexCollection of the specified index from the ListBox.ObjectCollection of the ListBox.

MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。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).

扩展方法

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.

适用于