How can I immediately/reactively determine if any CheckedBoxListItem has been selected? [duplicate]

有些话、适合烂在心里 提交于 2019-12-11 10:11:51

问题


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;
    . . .

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?


回答1:


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();
    });
}


来源:https://stackoverflow.com/questions/34421735/how-can-i-immediately-reactively-determine-if-any-checkedboxlistitem-has-been-se

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!