CheckedListBox.SetItemCheckState(Int32, CheckState) Metoda

Definicja

Ustawia stan sprawdzania elementu pod określonym indeksem.Sets the check state of the item at the specified index.

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)

Parametry

index
Int32

Indeks elementu, dla którego ma zostać ustawiony stan.The index of the item to set the state for.

value
CheckState

Jedna z CheckState wartości.One of the CheckState values.

Wyjątki

indexOkreślona wartość jest mniejsza od zera.The index specified is less than zero.

-lub--or- indexJest większa lub równa liczbie elementów na liście.The index is greater than or equal to the count of items in the list.

valueNie jest jedną z CheckState wartości.The value is not one of the CheckState values.

Przykłady

Poniższy przykład wylicza elementy w CheckedListBox i sprawdza każdy inny element na liście.The following example enumerates the items in the CheckedListBox and checks every other item in the list. W przykładzie pokazano, SetItemCheckState jak użyć SetItemChecked metod i, aby ustawić stan zaznaczenia elementu.The example demonstrates using the SetItemCheckState and SetItemChecked methods to set the check state of an item. Dla każdego innego elementu, który ma zostać sprawdzony, SetItemCheckState jest wywoływana, aby ustawić CheckState do Indeterminate , podczas gdy SetItemChecked jest wywoływana dla drugiego elementu, aby ustawić stan zaznaczenia Checked .For every other item that is to be checked, SetItemCheckState is called to set the CheckState to Indeterminate, while SetItemChecked is called on the other item to set the checked state to Checked.

W przykładzie pokazano również, Items jak użyć właściwości, aby uzyskać CheckedListBox.ObjectCollection Count elementy.The example also demonstrates using the Items property to get the CheckedListBox.ObjectCollection to get the Count of items.

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

Uwagi

SetItemCheckStateMetoda wywołuje ItemCheck zdarzenie.The SetItemCheckState method raises the ItemCheck event.

Elementy CheckState , których wartość jest Indeterminate wyświetlana z znacznikiem wyboru w polu wyboru, ale pole jest wyszarzone, aby wskazać nieokreślony stan zaznaczonego elementu.Items whose CheckState is set to Indeterminate appear with a check mark in the check box, but the box is grayed to indicate the indeterminate status of the checked item.

Dotyczy

Zobacz też