CheckedListBox.SetItemCheckState(Int32, CheckState) 方法

定义

设置指定索引处的项的复选状态。

public:
 void SetItemCheckState(int index, System::Windows::Forms::CheckState value);
public void SetItemCheckState (int index, System.Windows.Forms.CheckState value);
member this.SetItemCheckState : int * System.Windows.Forms.CheckState -> unit
Public Sub SetItemCheckState (index As Integer, value As CheckState)

参数

index
Int32

要为其设置状态的项的索引。

value
CheckState

CheckState 值之一。

例外

指定的 index 小于零。

- 或 -

index 大于或等于列表中的项的计数。

value 不是 CheckState 值中的一个。

示例

以下示例枚举 中的 CheckedListBox 项,并检查列表中的所有其他项。 该示例演示如何使用 SetItemCheckStateSetItemChecked 方法来设置项的检查状态。 对于要检查的任何其他项, SetItemCheckState 调用 以将 设置为 CheckStateIndeterminate,而 SetItemChecked 对另一项调用 以将选中状态设置为 Checked

该示例还演示如何使用 Items 属性获取 CheckedListBox.ObjectCollectionCount 的 。

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;
}
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;
}
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

注解

SetItemCheckState 方法引发 ItemCheck 事件。

CheckState Indeterminate设置为在检查框中显示带有检查标记的项,但该框灰显以指示已选中项的不确定状态。

适用于

另请参阅