CheckedListBox.CheckedItemCollection 類別

定義

將已選取項目的集合 (包括處於不定狀態的項目) 封裝至 CheckedListBox 控制項中。Encapsulates the collection of checked items, including items in an indeterminate state, in a CheckedListBox control.

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

範例

下列範例會列舉中CheckedListBox.CheckedIndexCollection的已核取專案, 以查看專案所在的檢查狀態。The following example enumerates the checked items in the CheckedListBox.CheckedIndexCollection to see what check state an item is in. 此範例示範如何使用GetItemCheckState方法來設定專案的檢查狀態。The example demonstrates using the GetItemCheckState method to set the check state of an item. 此範例也會示範如何CheckedIndices使用屬性來CheckedListBox.CheckedIndexCollection取得和CheckedItems屬性, 以取得CheckedListBox.CheckedItemCollectionThe example also demonstrates using the CheckedIndices property to get the CheckedListBox.CheckedIndexCollection, and the CheckedItems property to get the CheckedListBox.CheckedItemCollection.

第一個迴圈會使用GetItemCheckState方法來CheckState取得每個已核取專案的, 並指定專案的索引。The first loop uses the GetItemCheckState method to get the CheckState of each checked item, given the index of the item. 第二個迴圈也GetItemCheckState會使用, 但是ListBox.ObjectCollection.IndexOf會使用方法來抓取專案的索引。The second loop also uses GetItemCheckState, but uses the ListBox.ObjectCollection.IndexOf method to retrieve the index for the item.

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控制項中所有專案的子集, 它只包含處於已核取或不定狀態的專案。The checked items collection is a subset of all items in the CheckedListBox control; it contains only those items that are in a checked or indeterminate state.

下表是控制項中的索引項目目集合 (包含在控制項中的所有專案) 的範例。The following table is an example of the indexed collection of items in the control (all items contained in the control).

索引Index 項目Item 檢查狀態Check State
00 物件1object 1 Unchecked
11 物件2object 2 Checked
22 物件3object 3 Unchecked
33 物件4object 4 Indeterminate
44 物件5object 5 Checked

根據上一個範例, 下表顯示已選取專案的索引集合。Based on the previous example, the following table shows the indexed collection of the checked items.

索引Index 項目Item
00 物件2object 2
11 物件4object 4
22 物件5object 5

類別有兩個成員, 可讓您存取預存的索引Item[Int32] 、屬性和IndexOf方法。 CheckedListBoxThe CheckedListBox class has two members that allow you to access the stored indexes, the Item[Int32] property and the IndexOf method.

根據先前的範例, 呼叫Item[Int32]參數值為1的屬性會傳回物件4。Based on the previous example, a call to the Item[Int32] property with a parameter value of 1 returns object 4. 使用物件 4 IndexOf的參數呼叫, 會傳回值1。A call to IndexOf with a parameter of object 4 returns a value of 1.

屬性

Count

取得集合中的項目數目。Gets the number of items in the collection.

IsReadOnly

取得值,指出集合是否為唯讀。Gets a value indicating if the collection is read-only.

Item[Int32]

取得已選取項目集合中的物件。Gets an object in the checked items collection.

方法

Contains(Object)

判斷指定的項目是否位於集合中。Determines whether the specified item is located in the collection.

CopyTo(Array, Int32)

將整個集合複製到現有陣列中的指定位置。Copies the entire collection into an existing array at a specified location within the array.

Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
GetEnumerator()

傳回列舉值,可用來逐一查看 CheckedItems 集合。Returns an enumerator that can be used to iterate through the CheckedItems collection.

GetHashCode()

作為預設雜湊函數。Serves as the default hash function.

(繼承來源 Object)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
IndexOf(Object)

傳回已選取項目集合內的索引。Returns an index into the collection of checked items.

MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)

明確介面實作

ICollection.IsSynchronized

如需這個成員的說明,請參閱 IsSynchronizedFor a description of this member, see IsSynchronized.

ICollection.SyncRoot

如需這個成員的說明,請參閱 SyncRootFor a description of this member, see SyncRoot.

IList.Add(Object)

如需這個成員的說明,請參閱 Add(Object)For a description of this member, see Add(Object).

IList.Clear()

如需這個成員的說明,請參閱 Clear()For a description of this member, see Clear().

IList.Insert(Int32, Object)

如需這個成員的說明,請參閱 Insert(Int32, Object)For a description of this member, see Insert(Int32, Object).

IList.IsFixedSize

如需這個成員的說明,請參閱 IsFixedSizeFor a description of this member, see IsFixedSize.

IList.Remove(Object)

如需這個成員的說明,請參閱 Remove(Object)For a description of this member, see Remove(Object).

IList.RemoveAt(Int32)

如需這個成員的說明,請參閱 RemoveAt(Int32)For a description of this member, see RemoveAt(Int32).

擴充方法

Cast<TResult>(IEnumerable)

IEnumerable 的項目轉換成指定的型別。Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable)

根據指定的型別來篩選 IEnumerable 的項目。Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable)

啟用查詢的平行化作業。Enables parallelization of a query.

AsQueryable(IEnumerable)

IEnumerable 轉換成 IQueryableConverts an IEnumerable to an IQueryable.

適用於