Windows的C#的CheckedListBox经过项目事件处理 [英] Windows C# CheckedListBox Checked Item Event Handling
问题描述
我目前正在开发使用CheckedListBoxes该计划的某些方面的窗口应用程序。我遇到的一个问题是,我一直在试图找到哪个事件,当一个项目被选中,这样我可以让任何时候列表项检查表单按钮被触发。
I'm currently developing a Window app that uses CheckedListBoxes for certain aspects of the program. A problem I've encountered is that I have been trying to find which event is triggered when an item is checked so that I can enable a form button when any list item is checked.
问题是,我试着用以下;
Problem is that I tried using the following;
private void clbAvailMods_ItemCheck(object sender, ItemCheckEventArgs e)
{
if(e.NewValue == CheckState.Checked)
{
btnInstall.Enabled = true;
}
}
但是当我设置一个断点的if语句,在检查列表框中的项目它永远不会触发。
but when I set a breakpoint on the if statement, it never fires upon checking an item in the listbox.
难道我做错了吗?
推荐答案
一个标准的Windows窗体诀窍是,直到所有事件的副作用已完成推迟运行的代码。你延迟运行的代码与Control.BeginInvoke()方法。这将解决您的问题:
A standard Windows Forms trick is to delay running code until all event side-effects have been completed. You delay running code with the Control.BeginInvoke() method. This will fix your problem:
private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e) {
this.BeginInvoke(new MethodInvoker(evalList), null);
}
private void evalList() {
bool any = false;
for (int ix = 0; ix < checkedListBox1.Items.Count; ++ix) {
if (checkedListBox1.GetItemChecked(ix)) {
any = true;
break;
}
}
btnInstall.Enabled = any;
}
这篇关于Windows的C#的CheckedListBox经过项目事件处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!