Export (0) Print
Expand All
0 out of 1 rated this helpful - Rate this topic

CheckedListBox.ObjectCollection Class

Represents the collection of items in a CheckedListBox.

For a list of all members of this type, see CheckedListBox.ObjectCollection Members.

System.Object
   System.Windows.Forms.ListBox.ObjectCollection
      System.Windows.Forms.CheckedListBox.ObjectCollection

[Visual Basic]
Public Class CheckedListBox.ObjectCollection
   Inherits ListBox.ObjectCollection
[C#]
public class CheckedListBox.ObjectCollection :
   ListBox.ObjectCollection
[C++]
public __gc class CheckedListBox.ObjectCollection : public
   ListBox.ObjectCollection
[JScript]
public class CheckedListBox.ObjectCollection extends
   ListBox.ObjectCollection

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

The collection is accessed from the parent control, CheckedListBox, by the Items property. To create a collection of objects to display in the CheckedListBox control, you can add or remove the items individually by using the Add and Remove methods.

Example

[Visual Basic, C#, C++] The following example enumerates the items in the CheckedListBox and checks every other item in the list. The example demonstrates using the Items property to get the CheckedListBox.ObjectCollection to get the Count of items.

[Visual Basic, C#, C++] The example also demonstrates using the SetItemCheckState and SetItemChecked methods to set the check state of an item. For every other item that is to be checked, SetItemCheckState is called to set the CheckState to Indeterminate, while SetItemChecked is called on the other item to set the checked state to Checked.

[Visual Basic] 
Private Sub CheckEveryOther_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckEveryOther.Click
    ' Cycle through every item and check every other.
    Dim i As Integer

    ' Set flag to true to know when this code is being executed. Used in the ItemCheck
    ' event handler.
    insideCheckEveryOther = True

    For i = 0 To CheckedListBox1.Items.Count - 1
        ' For every other item in the list, set as checked.

        If ((i Mod 2) = 0) Then
            ' But for each other item that is to be checked, set as being in an
            ' indeterminate checked state.

            If ((i Mod 4) = 0) Then
                CheckedListBox1.SetItemCheckState(i, CheckState.Indeterminate)
            Else
                CheckedListBox1.SetItemChecked(i, True)
            End If
        End If
    Next

    insideCheckEveryOther = False

End Sub

[C#] 
private void CheckEveryOther_Click(object sender, System.EventArgs e) {
    // Cycle through every item and check every other.

    // Set flag to true to know when this code is being executed. Used in the ItemCheck
    // event handler.
    insideCheckEveryOther = true;

    for (int i = 0; i < checkedListBox1.Items.Count; i++) {
        // For every other item in the list, set as checked.
        if ((i % 2) == 0) {
            // But for each other item that is to be checked, set as being in an
            // indeterminate checked state.
            if ((i % 4) == 0)
                checkedListBox1.SetItemCheckState(i, CheckState.Indeterminate);
            else
                checkedListBox1.SetItemChecked(i, true);
        }
    }        

    insideCheckEveryOther = false;
}

[C++] 
private:
    void CheckEveryOther_Click(Object* /*sender*/, System::EventArgs* /*e*/) {
        // Cycle through every item and check every other.

        // Set flag to true to know when this code is being executed. Used in the ItemCheck
        // event handler.
        insideCheckEveryOther = true;

        for (int i = 0; i < checkedListBox1->Items->Count; i++) {
            // For every other item in the list, set as checked.
            if ((i % 2) == 0) {
                // But for each other item that is to be checked, set as being in an
                // indeterminate checked state.
                if ((i % 4) == 0)
                    checkedListBox1->SetItemCheckState(i, CheckState::Indeterminate);
                else
                    checkedListBox1->SetItemChecked(i, true);
            }
        }        

        insideCheckEveryOther = false;
    }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Windows.Forms

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Assembly: System.Windows.Forms (in System.Windows.Forms.dll)

See Also

CheckedListBox.ObjectCollection Members | System.Windows.Forms Namespace

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.