如何立即/反应确定是否已选择任何CheckedBoxListItem? [英] How can I immediately/reactively determine if any CheckedBoxListItem has been selected?

查看:86
本文介绍了如何立即/反应确定是否已选择任何CheckedBoxListItem?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仅在首先选择有效条件时才希望启用按钮(C#Windows Forms应用程序).我有这段代码(我先尝试了IndexChanged和ValueChanged事件,但是此答案表示ItemCheck事件是要监视的事件:

I want to enable a button only if valid criteria have first been selected (C# Windows Forms app). I have this code (I tried the IndexChanged and ValueChanged events first, but this answer indicates the ItemCheck event is the one to monitor:

private void checkedListBoxUnits_ItemCheck(object sender, ItemCheckEventArgs iceargs)
{
    buttonGenRpts.Enabled = ValidSelections();
}

private bool ValidSelections()
{
    bool OneUnitSelected = checkedListBoxUnits.CheckedItems.Count == 1;
    . . .

即使在checkedListBoxUnits控件中选择一个项目(复选框控件),OneUnitSelected始终为false.这些事件似乎在实际选中该复选框之前触发.那么我可以利用什么事件来验证项目是否已在CheckedListBox中选中?

OneUnitSelected is always false, even after selecting an item (checkbox control) in the checkedListBoxUnits control. It seems that these events fire before the checkbox is actually checked. So what event can I tap into to verify an item has been checked in a CheckedListBox?

推荐答案

这有点棘手,但是您可以将ValidSelections推迟运行,直到检查完成:

This is a bit hacky, but you could defer running ValidSelections until the checking is complete:

private void checkedListBoxUnits_ItemCheck(object sender, ItemCheckEventArgs iceargs)
{
    BeginInvoke(() => {
        buttonGenRpts.Enabled = ValidSelections();
    });
}

这篇关于如何立即/反应确定是否已选择任何CheckedBoxListItem?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆