CheckedListBox.SetItemChecked(Int32, Boolean) 方法

定义

将指定索引处的项的 CheckState 设置为 Checked

public:
 void SetItemChecked(int index, bool value);
public void SetItemChecked (int index, bool value);
member this.SetItemChecked : int * bool -> unit
Public Sub SetItemChecked (index As Integer, value As Boolean)

参数

index
Int32

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

value
Boolean

若要将该项设置为选中,则为 true;否则为 false

例外

指定的 index 小于零。

  • 或 -

该索引大于列表中项的计数。

示例

以下示例枚举列表中的 CheckedListBox 项并检查列表中的所有其他项。 该示例演示如何使用 SetItemCheckStateSetItemChecked 方法设置项的检查状态。 对于要检查的每个其他项,SetItemCheckState将调用设置为IndeterminateCheckState,而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

注解

传递值 true 时,此方法将 CheckState 值设置为 Checked。 A value of false sets CheckState to Unchecked.

适用于