CheckedListBox.CheckedIndexCollection 類別

定義

將已核取項目的索引集合 (包括處於不定狀態的項目) 封裝至 CheckedListBox 中。

public: ref class CheckedListBox::CheckedIndexCollection : System::Collections::IList
public class CheckedListBox.CheckedIndexCollection : System.Collections.IList
type CheckedListBox.CheckedIndexCollection = class
    interface IList
    interface ICollection
    interface IEnumerable
Public Class CheckedListBox.CheckedIndexCollection
Implements IList
繼承
CheckedListBox.CheckedIndexCollection
實作

範例

下列範例會列舉 中的 CheckedListBox.CheckedIndexCollection 已核取專案,以查看專案所在的檢查狀態。 此範例示範如何使用 GetItemCheckState 方法來設定專案的檢查狀態。 此範例也會示範如何使用 CheckedIndices 屬性來取得 CheckedListBox.CheckedIndexCollection ,以及 CheckedItems 取得 CheckedListBox.CheckedItemCollection 的屬性。

第一個迴圈會 GetItemCheckState 使用 方法來取得 CheckState 每個已檢查項目的 ,並指定專案的索引。 第二個迴圈也會使用 GetItemCheckState ,但會使用 ListBox.ObjectCollection.IndexOf 方法來擷取專案的索引。

void WhatIsChecked_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
{
   
   // Display in a message box all the items that are checked.
   // First show the index and check state of all selected items.
   IEnumerator^ myEnum1 = checkedListBox1->CheckedIndices->GetEnumerator();
   while ( myEnum1->MoveNext() )
   {
      Int32 indexChecked =  *safe_cast<Int32^>(myEnum1->Current);
      
      // The indexChecked variable contains the index of the item.
      MessageBox::Show( String::Concat( "Index#: ", indexChecked, ", is checked. Checked state is: ", checkedListBox1->GetItemCheckState( indexChecked ), "." ) );
   }

   
   // Next show the Object* title and check state for each item selected.
   IEnumerator^ myEnum2 = checkedListBox1->CheckedItems->GetEnumerator();
   while ( myEnum2->MoveNext() )
   {
      Object^ itemChecked = safe_cast<Object^>(myEnum2->Current);
      
      // Use the IndexOf method to get the index of an item.
      MessageBox::Show( String::Concat( "Item with title: \"", itemChecked, "\", is checked. Checked state is: ", checkedListBox1->GetItemCheckState( checkedListBox1->Items->IndexOf( itemChecked ) ), "." ) );
   }
}
private void WhatIsChecked_Click(object sender, System.EventArgs e) {
    // Display in a message box all the items that are checked.

    // First show the index and check state of all selected items.
    foreach(int indexChecked in checkedListBox1.CheckedIndices) {
        // The indexChecked variable contains the index of the item.
        MessageBox.Show("Index#: " + indexChecked.ToString() + ", is checked. Checked state is:" +
                        checkedListBox1.GetItemCheckState(indexChecked).ToString() + ".");
    }

    // Next show the object title and check state for each item selected.
    foreach(object itemChecked in checkedListBox1.CheckedItems) {

        // Use the IndexOf method to get the index of an item.
        MessageBox.Show("Item with title: \"" + itemChecked.ToString() +
                        "\", is checked. Checked state is: " +
                        checkedListBox1.GetItemCheckState(checkedListBox1.Items.IndexOf(itemChecked)).ToString() + ".");
    }
}
Private Sub WhatIsChecked_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles WhatIsChecked.Click
    ' Display in a message box all the items that are checked.
    Dim indexChecked As Integer
    Dim itemChecked As Object
    Const quote As String = """"

    ' First show the index and check state of all selected items.
    For Each indexChecked In CheckedListBox1.CheckedIndices
        ' The indexChecked variable contains the index of the item.
        MessageBox.Show("Index#: " + indexChecked.ToString() + ", is checked. Checked state is:" + _
                        CheckedListBox1.GetItemCheckState(indexChecked).ToString() + ".")
    Next

    ' Next show the object title and check state for each item selected.
    For Each itemChecked In CheckedListBox1.CheckedItems

        ' Use the IndexOf method to get the index of an item.
        MessageBox.Show("Item with title: " + quote + itemChecked.ToString() + quote + _
                        ", is checked. Checked state is: " + _
                        CheckedListBox1.GetItemCheckState(CheckedListBox1.Items.IndexOf(itemChecked)).ToString() + ".")
    Next

End Sub

備註

核取的索引集合是 控制項中所有專案的集合中 CheckedListBox 索引的子集。 這些索引會指定處於已核取或不確定狀態的專案。

下表是 控制項中專案的索引集合範例, (控制項中包含的所有專案) 。

索引 Item 檢查狀態
0 物件 1 Unchecked
1 物件 2 Checked
2 物件 3 Unchecked
3 物件 4 Indeterminate
4 物件 5 Checked

下表根據上一個範例顯示已檢查項目的索引索引集合。

索引 專案索引
0 1
1 3
2 4

類別 CheckedListBox 有兩個成員,可讓您存取預存索引、 Item[] 屬性和 IndexOf 方法。

根據上一個範例,對參數值為 1 的屬性呼叫 Item[] 會傳回值 3。 參數為 3 的 IndexOf 呼叫會傳回值 1。

屬性

Count

取得已選取項目的數目。

IsReadOnly

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

Item[Int32]

取得 CheckedListBox 控制項中已選取項目的索引。

方法

Contains(Int32)

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

CopyTo(Array, Int32)

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

Equals(Object)

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

(繼承來源 Object)
GetEnumerator()

傳回列舉值,可用來逐一查看 CheckedIndices 集合。

GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
IndexOf(Int32)

傳回已選取索引集合內的索引。

MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

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

(繼承來源 Object)

明確介面實作

ICollection.IsSynchronized

取得值,這個值表示對 CheckedListBox.CheckedIndexCollection 的存取是否同步 (安全執行緒)。

ICollection.SyncRoot

取得可用來對控制項集合進行同步 (Synchronize) 存取的物件。 如需這個成員的說明,請參閱 SyncRoot

IList.Add(Object)

此 API 支援此產品基礎結構,但無法直接用於程式碼之中。

將項目加入至 CheckedListBox.CheckedIndexCollection。 如需這個成員的說明,請參閱 Add(Object)

IList.Clear()

CheckedListBox.CheckedIndexCollection 中移除所有項目。 如需這個成員的說明,請參閱 Clear()

IList.Contains(Object)

判斷指定的索引是否位於 CheckedListBox.CheckedIndexCollection 中。 如需這個成員的說明,請參閱 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

適用於

另請參閱