Condividi tramite


CheckedListBox.CheckedIndexCollection Classe

Definizione

Incapsula la raccolta di indici di elementi selezionati (inclusi gli elementi in uno stato indeterminato) in un oggetto CheckedListBox.

public: ref class CheckedListBox::CheckedIndexCollection : System::Collections::IList
public class CheckedListBox.CheckedIndexCollection : System.Collections.IList
type CheckedListBox.CheckedIndexCollection = class
    interface IList
    interface ICollection
    interface IEnumerable
Public Class CheckedListBox.CheckedIndexCollection
Implements IList
Ereditarietà
CheckedListBox.CheckedIndexCollection
Implementazioni

Esempio

Nell'esempio seguente vengono enumerati gli elementi selezionati in CheckedListBox.CheckedIndexCollection per verificare lo stato di controllo in cui si trova un elemento. Nell'esempio viene illustrato l'utilizzo del GetItemCheckState metodo per impostare lo stato di controllo di un elemento. L'esempio illustra anche l'uso della CheckedIndices proprietà per ottenere l'oggetto CheckedListBox.CheckedIndexCollectione la CheckedItems proprietà per ottenere .CheckedListBox.CheckedItemCollection

Il primo ciclo usa il GetItemCheckState metodo per ottenere l'oggetto CheckState di ogni elemento selezionato, in base all'indice dell'elemento. Il secondo ciclo usa GetItemCheckStateanche , ma usa il ListBox.ObjectCollection.IndexOf metodo per recuperare l'indice per l'elemento.

void WhatIsChecked_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
{
   
   // Display in a message box all the items that are checked.
   // First show the index and check state of all selected items.
   IEnumerator^ myEnum1 = checkedListBox1->CheckedIndices->GetEnumerator();
   while ( myEnum1->MoveNext() )
   {
      Int32 indexChecked =  *safe_cast<Int32^>(myEnum1->Current);
      
      // The indexChecked variable contains the index of the item.
      MessageBox::Show( String::Concat( "Index#: ", indexChecked, ", is checked. Checked state is: ", checkedListBox1->GetItemCheckState( indexChecked ), "." ) );
   }

   
   // Next show the Object* title and check state for each item selected.
   IEnumerator^ myEnum2 = checkedListBox1->CheckedItems->GetEnumerator();
   while ( myEnum2->MoveNext() )
   {
      Object^ itemChecked = safe_cast<Object^>(myEnum2->Current);
      
      // Use the IndexOf method to get the index of an item.
      MessageBox::Show( String::Concat( "Item with title: \"", itemChecked, "\", is checked. Checked state is: ", checkedListBox1->GetItemCheckState( checkedListBox1->Items->IndexOf( itemChecked ) ), "." ) );
   }
}
private void WhatIsChecked_Click(object sender, System.EventArgs e) {
    // Display in a message box all the items that are checked.

    // First show the index and check state of all selected items.
    foreach(int indexChecked in checkedListBox1.CheckedIndices) {
        // The indexChecked variable contains the index of the item.
        MessageBox.Show("Index#: " + indexChecked.ToString() + ", is checked. Checked state is:" +
                        checkedListBox1.GetItemCheckState(indexChecked).ToString() + ".");
    }

    // Next show the object title and check state for each item selected.
    foreach(object itemChecked in checkedListBox1.CheckedItems) {

        // Use the IndexOf method to get the index of an item.
        MessageBox.Show("Item with title: \"" + itemChecked.ToString() +
                        "\", is checked. Checked state is: " +
                        checkedListBox1.GetItemCheckState(checkedListBox1.Items.IndexOf(itemChecked)).ToString() + ".");
    }
}
Private Sub WhatIsChecked_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles WhatIsChecked.Click
    ' Display in a message box all the items that are checked.
    Dim indexChecked As Integer
    Dim itemChecked As Object
    Const quote As String = """"

    ' First show the index and check state of all selected items.
    For Each indexChecked In CheckedListBox1.CheckedIndices
        ' The indexChecked variable contains the index of the item.
        MessageBox.Show("Index#: " + indexChecked.ToString() + ", is checked. Checked state is:" + _
                        CheckedListBox1.GetItemCheckState(indexChecked).ToString() + ".")
    Next

    ' Next show the object title and check state for each item selected.
    For Each itemChecked In CheckedListBox1.CheckedItems

        ' Use the IndexOf method to get the index of an item.
        MessageBox.Show("Item with title: " + quote + itemChecked.ToString() + quote + _
                        ", is checked. Checked state is: " + _
                        CheckedListBox1.GetItemCheckState(CheckedListBox1.Items.IndexOf(itemChecked)).ToString() + ".")
    Next

End Sub

Commenti

L'insieme di indici controllati è un subset degli indici nell'insieme di tutti gli elementi nel CheckedListBox controllo . Questi indici specificano gli elementi in uno stato selezionato o indeterminato.

La tabella seguente è un esempio della raccolta indicizzata di elementi nel controllo (tutti gli elementi contenuti nel controllo).

Indice Elemento Controlla stato
0 oggetto 1 Unchecked
1 oggetto 2 Checked
2 oggetto 3 Unchecked
3 oggetto 4 Indeterminate
4 oggetto 5 Checked

In base all'esempio precedente, nella tabella seguente viene illustrata la raccolta indicizzata di indici di elementi controllati.

Indice Indice dell'elemento
0 1
1 3
2 4

La CheckedListBox classe ha due membri che consentono di accedere agli indici archiviati, alla Item[] proprietà e al IndexOf metodo .

In base all'esempio precedente, una chiamata alla proprietà con un valore di parametro pari a 1 restituisce un valore pari a Item[] 3. Una chiamata a con un parametro 3 restituisce un valore pari a IndexOf 1.

Proprietà

Nome Descrizione
Count

Ottiene il numero di elementi selezionati.

IsReadOnly

Ottiene un valore che indica se l'insieme è di sola lettura.

Item[Int32]

Ottiene l'indice di un elemento selezionato nel CheckedListBox controllo .

Metodi

Nome Descrizione
Contains(Int32)

Determina se l'indice specificato si trova nella raccolta.

CopyTo(Array, Int32)

Copia l'intera raccolta in una matrice esistente in una posizione specificata all'interno della matrice.

Equals(Object)

Determina se l'oggetto specificato è uguale all'oggetto corrente.

(Ereditato da Object)
GetEnumerator()

Restituisce un enumeratore che può essere utilizzato per scorrere l'insieme CheckedIndices .

GetHashCode()

Funge da funzione hash predefinita.

(Ereditato da Object)
GetType()

Ottiene il Type dell'istanza corrente.

(Ereditato da Object)
IndexOf(Int32)

Restituisce un indice nell'insieme di indici controllati.

MemberwiseClone()

Crea una copia superficiale del Objectcorrente.

(Ereditato da Object)
ToString()

Restituisce una stringa che rappresenta l'oggetto corrente.

(Ereditato da Object)

Implementazioni dell'interfaccia esplicita

Nome Descrizione
ICollection.IsSynchronized

Ottiene un valore che indica se l'accesso CheckedListBox.CheckedIndexCollection a è sincronizzato (thread-safe).

ICollection.SyncRoot

Ottiene un oggetto che può essere utilizzato per sincronizzare l'accesso all'insieme di controlli. Per una descrizione di questo membro, vedere SyncRoot.

IList.Add(Object)

Questa API supporta l'infrastruttura del prodotto e non è previsto che venga usata direttamente dal codice.

Aggiunge un elemento all'oggetto CheckedListBox.CheckedIndexCollection. Per una descrizione di questo membro, vedere Add(Object).

IList.Clear()

Rimuove tutti gli elementi da CheckedListBox.CheckedIndexCollection. Per una descrizione di questo membro, vedere Clear().

IList.Contains(Object)

Determina se l'indice specificato si trova all'interno di CheckedListBox.CheckedIndexCollection. Per una descrizione di questo membro, vedere Contains(Object).

IList.IndexOf(Object)

Per una descrizione di questo membro, vedere IndexOf(Object).

IList.Insert(Int32, Object)

Per una descrizione di questo membro, vedere Insert(Int32, Object).

IList.IsFixedSize

Per una descrizione di questo membro, vedere IsFixedSize.

IList.Item[Int32]

Per una descrizione di questo membro, vedere Item[Int32].

IList.Remove(Object)

Per una descrizione di questo membro, vedere Remove(Object).

IList.RemoveAt(Int32)

o una descrizione di questo membro, vedere RemoveAt(Int32).

Metodi di estensione

Nome Descrizione
AsParallel(IEnumerable)

Abilita la parallelizzazione di una query.

AsQueryable(IEnumerable)

Converte un IEnumerable in un IQueryable.

Cast<TResult>(IEnumerable)

Esegue il cast degli elementi di un IEnumerable al tipo specificato.

OfType<TResult>(IEnumerable)

Filtra gli elementi di un IEnumerable in base a un tipo specificato.

Si applica a

Vedi anche