ListBox.SelectedIndices 属性

定义

获取一个集合,该集合包含 ListBox 中所有当前选定项的从零开始的索引。Gets a collection that contains the zero-based indexes of all currently selected items in the ListBox.

public:
 property System::Windows::Forms::ListBox::SelectedIndexCollection ^ SelectedIndices { System::Windows::Forms::ListBox::SelectedIndexCollection ^ get(); };
[System.ComponentModel.Browsable(false)]
public System.Windows.Forms.ListBox.SelectedIndexCollection SelectedIndices { get; }
member this.SelectedIndices : System.Windows.Forms.ListBox.SelectedIndexCollection
Public ReadOnly Property SelectedIndices As ListBox.SelectedIndexCollection

属性值

ListBox.SelectedIndexCollection,包含控件中当前选定项的索引。A ListBox.SelectedIndexCollection containing the indexes of the currently selected items in the control. 如果当前没有选定的项,则返回空 ListBox.SelectedIndexCollectionIf no items are currently selected, an empty ListBox.SelectedIndexCollection is returned.

属性

示例

下面的代码示例演示如何使用 FindString 方法来搜索 ListBox的项中搜索文本的所有实例。The following code example demonstrates how to use the FindString method to search for all instances of the search text in the items of the ListBox. 该示例使用 FindString 方法的版本,通过该方法可以指定起始搜索索引,以便对 ListBox中的所有项进行持续搜索。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. 该示例还演示了如何确定当 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,此属性将返回一个集合,其中包含在 ListBox中选择的所有项的索引。For a multiple-selection ListBox, this property returns a collection containing the indexes to all items that are selected in the ListBox. 对于单项选择 ListBox,此属性返回包含单个元素的集合,该元素包含 ListBox 中唯一选定项的索引。For a single-selection ListBox, this property returns a collection containing a single element containing the index of the only selected item in the ListBox. 有关如何操作集合项的详细信息,请参阅 ListBox.SelectedIndexCollectionFor more information about how to manipulate the items of the collection, see ListBox.SelectedIndexCollection.

ListBox 类提供了许多用于引用所选项目的方法。The ListBox class provides a number of ways to reference selected items. 您可以使用 SelectedIndex 属性,而不是使用 SelectedIndices 属性获取单选 ListBox中当前选定项的索引位置。Instead of using the SelectedIndices property to obtain the index position of the currently selected item in a single-selection ListBox, you can use the SelectedIndex property. 如果要获取 ListBox中当前选定的项,而不是项的索引位置,请使用 SelectedItem 属性。If you want to obtain the item that is currently selected in the ListBox, instead of the index position of the item, use the SelectedItem property. 此外,如果想要获取多选 ListBox中的所有选定项,则可以使用 "SelectedItems" 属性。In addition, you can use the SelectedItems property if you want to obtain all the selected items in a multiple-selection ListBox.

适用于

另请参阅