2010-11-17 7 views

Répondre

3

Dans gestionnaire d'événements ItemCheck utilisant ItemCheckEventArgs e vous pouvez retrive correspondant objet

checkedListBox1.Items[e.Index] 
3

est ici un code bare-bones qui devrait faire l'affaire:

public void CheckedListBox1_ItemCheck(object sender, ItemCheckEventArgs e) 
{ 
    var checkedListBox = (CheckedListBox)sender; 
    var checkedItemText = checkedListBox.Items[e.Index].ToString(); 
} 
0

dans l'événement SelectedIndexChanged, mettre le code suivant

string text = (sender as CheckedListBox).SelectedItem.ToString(); 
+1

Cela échouera si CheckedListBox prend en charge plusieurs sélections ... –

0

LeLa classea une propriété CheckedItems.

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() + "."); 
    } 

} 
+0

Cela lui donnera tous les éléments cochés. Le PO a spécifiquement déclaré qu'il voulait le texte de l'article qui a déclenché l'événement ... –

+0

@Justin Niessner: Vrai. Et il montre également comment obtenir un seul élément à travers la propriété indexée 'Items':' checkedListBox1.Items.IndexOf (itemChecked)). ToString() '. Cela imprime l'élément exact énuméré, de sorte que l'OP peut voir les différentes collections à jouer avec l'objet 'CheckedListBox', et il peut voir comment en obtenir un. Deux oiseaux avec une pierre! –