CheckedListBox.CheckedItemCollection 类     
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
封装 CheckedListBox 控件中选中项(包括处于不确定状态的项)的集合。
public: ref class CheckedListBox::CheckedItemCollection : System::Collections::IListpublic class CheckedListBox.CheckedItemCollection : System.Collections.IListtype CheckedListBox.CheckedItemCollection = class
    interface IList
    interface ICollection
    interface IEnumerablePublic Class CheckedListBox.CheckedItemCollection
Implements IList- 继承
- 
				CheckedListBox.CheckedItemCollection
- 实现
示例
以下示例枚举 中 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 | 对象 2 | 
| 1 | 对象 4 | 
| 2 | 对象 5 | 
类 CheckedListBox 具有两个成员,可用于访问存储的索引、 Item[] 属性和 IndexOf 方法。
根据前面的示例,对 Item[] 参数值为 1 的 属性的调用将返回对象 4。 使用对象 4 的参数调用 IndexOf 将返回值 1。
属性
| Count | 获取集合中的项数。 | 
| IsReadOnly | 获取指示该集合是否为只读的值。 | 
| Item[Int32] | 获取选中项集合中的一个对象。 | 
方法
| Contains(Object) | 确定指定项是否位于集合内。 | 
| CopyTo(Array, Int32) | 将整个集合复制到现有数组中,从该数组内的指定位置开始复制。 | 
| Equals(Object) | 确定指定对象是否等于当前对象。(继承自 Object) | 
| GetEnumerator() | 返回一个可用于循环访问 CheckedItems 集合的枚举数。 | 
| GetHashCode() | 作为默认哈希函数。(继承自 Object) | 
| GetType() | 获取当前实例的 Type。(继承自 Object) | 
| IndexOf(Object) | 返回选中项集合中的一个索引。 | 
| MemberwiseClone() | 创建当前 Object 的浅表副本。(继承自 Object) | 
| ToString() | 返回表示当前对象的字符串。(继承自 Object) | 
显式接口实现
| ICollection.IsSynchronized | 有关此成员的说明,请参见 IsSynchronized。 | 
| ICollection.SyncRoot | 有关此成员的说明,请参见 SyncRoot。 | 
| IList.Add(Object) | 有关此成员的说明,请参见 Add(Object)。 | 
| IList.Clear() | 有关此成员的说明,请参见 Clear()。 | 
| IList.Insert(Int32, Object) | 有关此成员的说明,请参见 Insert(Int32, Object)。 | 
| IList.IsFixedSize | 有关此成员的说明,请参见 IsFixedSize。 | 
| IList.Remove(Object) | 有关此成员的说明,请参见 Remove(Object)。 | 
| IList.RemoveAt(Int32) | 有关此成员的说明,请参见 RemoveAt(Int32)。 | 
扩展方法
| Cast<TResult>(IEnumerable) | 将 IEnumerable 的元素强制转换为指定的类型。 | 
| OfType<TResult>(IEnumerable) | 根据指定类型筛选 IEnumerable 的元素。 | 
| AsParallel(IEnumerable) | 启用查询的并行化。 | 
| AsQueryable(IEnumerable) | 将 IEnumerable 转换为 IQueryable。 |