ListBox.SelectedIndexCollection 類別

定義

表示包含在 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.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.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 方法來判斷 儲存 中 ListBox.ObjectCollectionListBox 特定索引位置的位置。

建構函式

ListBox.SelectedIndexCollection(ListBox)

初始化 ListBox.SelectedIndexCollection 類別的新執行個體。

屬性

Count

取得集合中的項目數目。

IsReadOnly

取得值,表示集合是否為唯讀。

Item[Int32]

取得這個集合中指定索引處的索引值。

方法

Add(Int32)

在指定索引位置上加入 ListBox

Clear()

移除集合中所有的控制項。

Contains(Int32)

判斷指定的索引是否位於集合中。

CopyTo(Array, Int32)

將整個集合複製到現有陣列中的指定位置。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetEnumerator()

傳回列舉值,其可在選取索引集合中重複。

GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
IndexOf(Int32)

ListBox.SelectedIndexCollectionListBox.ObjectCollection 傳回指定索引在 ListBox 中的索引。

MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
Remove(Int32)

從集合中移除指定的控制項。

ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

明確介面實作

ICollection.IsSynchronized

如需這個成員的說明,請參閱 IsSynchronized

ICollection.SyncRoot

如需這個成員的說明,請參閱 SyncRoot

IList.Add(Object)

如需這個成員的說明,請參閱 Add(Object)

IList.Clear()

如需這個成員的說明,請參閱 Clear()

IList.Contains(Object)

如需這個成員的說明,請參閱 Contains(Object)

IList.IndexOf(Object)

如需這個成員的說明,請參閱 IndexOf(Object)

IList.Insert(Int32, Object)

如需這個成員的說明,請參閱 Insert(Int32, Object)

IList.IsFixedSize

如需這個成員的說明,請參閱 IsFixedSize

IList.Item[Int32]

如需這個成員的說明,請參閱 Item[Int32]

IList.Remove(Object)

如需這個成員的說明,請參閱 Remove(Object)

IList.RemoveAt(Int32)

如需這個成員的說明,請參閱 RemoveAt(Int32)

擴充方法

Cast<TResult>(IEnumerable)

IEnumerable 的項目轉換成指定的型別。

OfType<TResult>(IEnumerable)

根據指定的型別來篩選 IEnumerable 的項目。

AsParallel(IEnumerable)

啟用查詢的平行化作業。

AsQueryable(IEnumerable)

IEnumerable 轉換成 IQueryable

適用於