CheckedListBox.CheckedItemCollection Class

Definition

封装 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
Inheritance
CheckedListBox.CheckedItemCollection
Implements

Examples

下面的示例将枚举 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

Remarks

Checked items 集合是 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 ItemItem 检查状态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 ItemItem
00 对象2object 2
11 对象4object 4
22 对象5object 5

CheckedListBox 类有两个成员,可用于访问存储的索引、Item[] 属性和 IndexOf 方法。The CheckedListBox class has two members that allow you to access the stored indexes, the Item[] property and the IndexOf method.

根据前面的示例,对参数值为1的 Item[] 属性的调用将返回对象4。Based on the previous example, a call to the Item[] 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.

Properties

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.

Methods

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.

(Inherited from Object)
GetEnumerator()

返回一个可用于循环访问 CheckedItems 集合的枚举数。Returns an enumerator that can be used to iterate through the CheckedItems collection.

GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
IndexOf(Object)

返回选中项集合中的一个索引。Returns an index into the collection of checked items.

MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)

Explicit Interface Implementations

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).

Extension Methods

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.

Applies to