如何使用C#在已检查的列表框中获取新选中项目的文本

时间:2010-11-17 15:48:31

标签: c# checkedlistbox

我正在使用ItemCheckEventArgs并且我可以从中获取索引值,但是从这个值我不知道如何查看文本是什么被检查。

4 个答案:

答案 0 :(得分:4)

这里有一些简单易用的代码:

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

答案 1 :(得分:3)

在使用ItemCheckEventArgs的ItemCheck事件处理程序中,您可以检索相应的对象

checkedListBox1.Items[e.Index]

答案 2 :(得分:1)

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

}

答案 3 :(得分:0)

SelectedIndexChanged事件中,输入以下代码

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