共用方式為


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 事件。

設定 CheckStateIndeterminate 在核取方塊中顯示核取記號的專案,但方塊呈現灰色,表示已核取專案的狀態不確定。

適用於

另請參閱