CheckedListBox.CheckedIndexCollection 类

定义

封装 CheckedListBox 中选中的项(包括处于不确定状态的项)的索引集合。Encapsulates the collection of indexes of checked items (including items in an indeterminate state) in a 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的选中项进行枚举, 以查看项所在的检查状态。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

注解

Checked 索引集合是CheckedListBox控件中所有项的集合中的索引的子集。The checked indexes collection is a subset of the indexes into the collection of all items in the CheckedListBox control. 这些索引指定处于选中状态或不确定状态的项。These indexes specify items 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).

IndexIndex 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 indexes of checked items.

IndexIndex 项的索引Index of Item
00 11
11 33
22 44

类有两个成员, 可用于访问存储的索引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.

根据前面的示例, 对参数值为 1 Item[Int32]的属性的调用将返回值3。Based on the previous example, a call to the Item[Int32] property with a parameter value of 1 returns a value of 3. IndexOf使用参数3对的调用将返回值1。A call to IndexOf with a parameter of 3 returns a value of 1.

属性

Count

获取选中项数。Gets the number of checked items.

IsReadOnly

获取一个值,该值指示集合是否为只读。Gets a value indicating whether the collection is read-only.

Item[Int32]

获取 CheckedListBox 控件中某选中项的索引。Gets the index of a checked item in the CheckedListBox control.

方法

Contains(Int32)

确定指定索引是否位于集合中。Determines whether the specified index 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()

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

GetHashCode()

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

(继承自 Object)
GetType()

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

(继承自 Object)
IndexOf(Int32)

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

MemberwiseClone()

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

(继承自 Object)
ToString()

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

(继承自 Object)

显式界面实现

ICollection.IsSynchronized

获取一个值,该值指示是否同步对 CheckedListBox.CheckedIndexCollection 的访问(线程安全)。Gets a value indicating whether access to the CheckedListBox.CheckedIndexCollection is synchronized (thread safe).

ICollection.SyncRoot

获取可用于同步控件集合访问的对象。Gets an object that can be used to synchronize access to the collection of controls. 有关此成员的说明,请参见 SyncRootFor a description of this member, see SyncRoot.

IList.Add(Object)

将某项添加到 CheckedListBox.CheckedIndexCollection 中。Adds an item to the CheckedListBox.CheckedIndexCollection. 有关此成员的说明,请参见 Add(Object)For a description of this member, see Add(Object).

IList.Clear()

CheckedListBox.CheckedIndexCollection 中移除所有项。Removes all items from the CheckedListBox.CheckedIndexCollection. 有关此成员的说明,请参见 Clear()For a description of this member, see Clear().

IList.Contains(Object)

确定指定索引是否位于 CheckedListBox.CheckedIndexCollection 内。Determines whether the specified index is located within the CheckedListBox.CheckedIndexCollection. 有关此成员的说明,请参见 Contains(Object)For a description of this member, see Contains(Object).

IList.IndexOf(Object)

有关此成员的说明,请参见 IndexOf(Object)For a description of this member, see IndexOf(Object).

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.Item[Int32]

有关此成员的说明,请参见 Item[Int32]For a description of this member, see Item[Int32].

IList.Remove(Object)

有关此成员的说明,请参见 Remove(Object)For a description of this member, see Remove(Object).

IList.RemoveAt(Int32)

或者此成员的说明(请参见 RemoveAt(Int32))。or 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.

适用于

另请参阅