CheckedListBox.SetItemChecked(Int32, Boolean) Méthode

Définition

Affecte au CheckState de l'élément au niveau de l'index spécifié la valeur Checked.Sets CheckState for the item at the specified index to 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)

Paramètres

index
Int32

Index de l'élément dont vous souhaitez définir l'état d'activation.The index of the item to set the check state for.

value
Boolean

true pour définir l'élément comme activé ; sinon, false.true to set the item as checked; otherwise, false.

Exceptions

L'index spécifié est inférieur à zéro.The index specified is less than zero.

-ou--or-

L'index est supérieur au nombre d'éléments dans la liste.The index is greater than the count of items in the list.

Exemples

L’exemple suivant énumère les éléments de la CheckedListBox et vérifie chaque autre élément de la liste.The following example enumerates the items in the CheckedListBox and checks every other item in the list. L’exemple illustre l’utilisation des méthodes SetItemCheckState et SetItemChecked pour définir l’état d’activation d’un élément.The example demonstrates using the SetItemCheckState and SetItemChecked methods to set the check state of an item. Pour tous les autres éléments à vérifier, SetItemCheckState est appelé pour définir la CheckState sur Indeterminate, tandis que SetItemChecked est appelé sur l’autre élément pour définir l’état activé sur 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.

L’exemple illustre également l’utilisation de la propriété Items pour récupérer le CheckedListBox.ObjectCollection afin d’extraire le Count d’éléments.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

Remarques

Quand une valeur de true est passée, cette méthode définit la valeur CheckState sur Checked.When a value of true is passed, this method sets the CheckState value to Checked. La valeur false définit CheckState à Unchecked.A value of false sets CheckState to Unchecked.

S’applique à