views:

65

answers:

4

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

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: 

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.

Will A
I already have the "CheckOnClick" value set to true. I noticed that Microsnot seems they have overlooked a rather important design element in this case. They'll let you establish what's GOING to change, but not trigger any events once the change is completed.I must say that those morons at MS could require a debugger for a wet dream.
rik_davis
A: 

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.

spinon
Yes, the ItemCheck does fire when you click a checkbox. The real problem is that the vhecked state of that checkbox will still be the opposite of what it was prior to clicking it until after the ItemCheck is finished processing. Hence, the true dilemma of attempting to see if a checkbox is actually checked or unchecked in realtime. i.e. the need for an "ItemCheckChanged" event.
rik_davis
So then can't you just check the opposite? If the itemcheck event is firing and the checkbox is not checked then that means that it is about to be checked correct?
spinon
A: 

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;
    }
Hans Passant
I'll most definitely be looking into this suggestion more. It seems to be on the right track on paper(screen). :)Thanks, Hans.
rik_davis
I think is is going to be the ticket I was looking for.Thank you for the assistance! :)
rik_davis
+1  A: 

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);
}
Sergio DomingoHernando