我正在使用ItemCheckEventArgs并且我可以从中获取索引值,但是从这个值我不知道如何查看文本是什么被检查。
答案 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();