ListBox.SelectedIndices 属性

获取一个集合,该集合包含 ListBox 中所有当前选定项的从零开始的索引。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
Public ReadOnly Property SelectedIndices As SelectedIndexCollection
用法
Dim instance As ListBox
Dim value As SelectedIndexCollection

value = instance.SelectedIndices
public SelectedIndexCollection SelectedIndices { get; }
public:
property SelectedIndexCollection^ SelectedIndices {
    SelectedIndexCollection^ get ();
}
/** @property */
public SelectedIndexCollection get_SelectedIndices ()
public function get SelectedIndices () : SelectedIndexCollection

属性值

ListBox.SelectedIndexCollection,包含控件中当前选定项的索引。如果当前没有选定的项,则返回空 ListBox.SelectedIndexCollection

备注

对于多重选择 ListBox,此属性返回一个集合,该集合包含 ListBox 中选定的所有项的索引。对于单项选择 ListBox,此属性返回一个包含单个元素的集合,该元素包含 ListBox 中唯一选定的项的索引。有关如何操作集合中的项的更多信息,请参见 ListBox.SelectedIndexCollection

ListBox 类提供若干引用选定项的方法。可以使用 SelectedIndex 属性,而不使用 SelectedIndices 属性获取单项选择 ListBox 中当前选定项的索引位置。如果要获取 ListBox 中当前选定的项,而不是该项的索引位置,请使用 SelectedItem 属性。另外,如果要获取多重选择 ListBox 中的所有选定项,可使用 SelectedItems 属性。

示例

下面的代码示例演示如何使用 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

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、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 类
ListBox 成员
System.Windows.Forms 命名空间
ListBox.SelectedIndex 属性
SelectedItem
SelectedItems