[英]User checked item in CheckedListBox
I have a CheckedListBox
. 我有一个CheckedListBox
。 I want to know when the user checked or unchecked an item. 我想知道用户何时选中或取消选中项目。 I tried using ItemCheck
event but it fires even when an item is programmatically checked. 我尝试使用ItemCheck
事件,但即使以编程方式检查项目也会触发。 How can I detect this? 我怎么能发现这个?
Using the ItemCheck
event handler is the correct method for detecting when the user ticks or un-ticks an item in the CheckedListBox
. 使用ItemCheck
事件处理程序是检测用户何时在CheckedListBox
或取消勾选项目的正确方法。 And yes, it will also fire when the item is checked/unchecked programmitically. 是的,它会在程序性地检查/取消选中项目时触发。
If you don't want the event fired when you set/unset items programmatically, you should remove the event handler before hand. 如果您不希望以编程方式设置/取消设置项时触发事件,则应事先删除事件处理程序。
Assuming your event handler looks like this: 假设您的事件处理程序如下所示:
private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
if (e.NewValue == CheckState.Checked)
{
Debug.Print("Checked");
}
else if (e.NewValue == CheckState.Unchecked)
{
Debug.Print("Un-Checked");
}
}
Before you set/unset items programmatically, you should add the line: 在以编程方式设置/取消设置项目之前,应添加以下行:
this.checkedListBox1.ItemCheck -= this.checkedListBox1_ItemCheck;
and after the items have been set/unset you in code, re-add the event handler with: 在代码中设置/取消设置项目后,请使用以下命令重新添加事件处理程序:
this.checkedListBox1.ItemCheck += this.checkedListBox1_ItemCheck;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.