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.ObjectCollection 如何存储 ListBox 的项的示例,以及这些项在示例 ListBox 中的选择状态。

索引

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 的项中搜索所有搜索文本的实例。该示例使用的 FindString 方法版本使您能够指定起始搜索索引,可从该索引继续搜索 ListBox 中的所有项。该示例还演示了如何确定在 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

线程安全

此类型的任何公共静态(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 命名空间