开发者

Windows C# CheckedListBox Checked Item Event Handling

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)
  开发者_StackOverflow      {
            btnInstall.Enabled = true;
        }
    }

but when I set a breakpoint on the if statement, it never fires upon checking an item in the listbox.

Am I doing something wrong here?


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


You can use the NewValue property to manually update CheckedItems.Count. This is the code I use to only enable a button when there's at least one item checked:

private void checkedListBoxProds_ItemCheck(object sender, ItemCheckEventArgs e)
{
    this.buttonGenerar.Enabled = ((this.checkedListBoxProds.CheckedItems.Count + (e.NewValue == CheckState.Checked ? 1 : -1)) > 0);
}


A couple of potential gotchas. Presumably you've added the event through the VS.Net GUI to ensure that it gets plumbed into the control. Try clicking on an item twice - once to give the item focus and again to toggle the check state - if you want an item to have its check state toggled on first click then set the "CheckOnClick" property to true.


I think it is the SelectedIndexChanged event but I will confirm right now.

EDIT: SelectedIndexChanged event does work. But that is firing regardless of whether the checkbox was checked. So I would then check the checked state if you want to do that.

But as an aside when I did use the ItemCheck event it did fire when I actually checked the checkbox and not just the text.


I know this has been answered long ago, but I found it easier to just handle the MouseUp and KeyUp events. The CheckedItems.Count property is accurate when those events are fired. Since they both do the same thing, I created a method to to the work and called that method from both event handlers.

private void clbFolders_KeyUp(object sender, KeyEventArgs e) { Update(); }
private void clbFolders_MouseUp(object sender, MouseEventArgs e) { Update(); }

private void Update()
{
    btnDelete.Enabled = clbFolders.CheckedItems.Count > 0;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜