test
test

Reputation: 2629

Reliable Way To Cancel ItemCheck Event On CheckedListBox

Does anyone know of a reliable way to cancel the ItemCheck event on a CheckedListBox? I am in a situation where the ItemCheck event should basically discard changes on a form, however, if the person decides to cancel the discard I would like the ItemCheck event not to fire so as not to change anything.

Upvotes: 5

Views: 3793

Answers (2)

alternatefaraz
alternatefaraz

Reputation: 374

public void cancelevent()       
{           
  checkedListBox1.ItemCheck -= new   ItemCheckEventHandler(this.checkedListBox1_ItemCheck);           
}

Call this method any where to cancel the event.

If we += the event will be created and -= will remove the event.

If you want more details please mention. :)

Upvotes: 0

Hans Passant
Hans Passant

Reputation: 941227

It is easy to do with the ItemCheck event. Just set the value back. Like this:

    private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e) {
        if (someCondition) e.NewValue = e.CurrentValue;
        else {
            // Regular stuff
            //...
        }
    }

Upvotes: 15

Related Questions