共用方式為


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 會呼叫 將 設定 CheckState Indeterminate 為 ,而 SetItemChecked 在其他專案上呼叫 則會將核取狀態設定為 Checked

此範例也會示範如何使用 Items 屬性來取得 CheckedListBox.ObjectCollection Count 專案的 。

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 。 的值 false 設定 CheckStateUnchecked

適用於