共用方式為


CheckedListBox.SetItemChecked(Int32, Boolean) 方法

定義

將指定索引的項目集合 CheckStateChecked

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

例外狀況

所指定的指數小於零。

-或-

索引大於清單中項目的數量。

範例

以下範例列舉了 中的 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

備註

當傳遞 的 true 值時,此方法將 的值設 CheckStateChecked。 集合到 falseCheckStateUnchecked的值。

適用於