ListBox.SelectedIndices 属性

定义

获取一个集合,该集合包含 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; }
[<System.ComponentModel.Browsable(false)>]
member this.SelectedIndices : System.Windows.Forms.ListBox.SelectedIndexCollection
Public ReadOnly Property SelectedIndices As ListBox.SelectedIndexCollection

属性值

ListBox.SelectedIndexCollection

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

属性

示例

下面的代码示例演示如何使用 FindString 该方法在项 ListBox中搜索搜索文本的所有实例。 该示例使用方法的版本 FindString ,可用于指定要从中连续搜索所有项 ListBox的起始搜索索引。 该示例还演示如何在 FindString 方法到达项列表底部后从列表顶部开始搜索,以防止递归搜索。 在找到 ListBox项后,将使用该方法 SetSelected 选择它们。

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的所有项。 对于单选 ListBox,此属性返回一个集合,该集合包含包含唯一选定项的索引的 ListBox单个元素。 有关如何操作集合项的详细信息,请参阅 ListBox.SelectedIndexCollection

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

适用于

另请参阅