0

I know that there is a checkedListBox event called ItemChecked and I think there's a CheckChanged event for individual checkboxes, but is there an ItemUnchecked or CheckChanged event counterpart for a checkedListBox? If so, how would I subscribe to it and implement/use it?

When I use the "NewValue" method recommended, it just spits back an error and states: "'EventArgs' does not contain a definition for 'NewValue' and no accessible extension method 'NewValue' accepting a first argument of type 'EventArgs' could be found (are you missing a using directive or an assembly reference?)". Does anyone have any advice?

My goal is to create a piece of code as a counterpart to the ItemChecked event so that if the user unchecks a box, it will undo the effect the checking event had.

Example of what I want to be able to do:

private void checkedListBoxBasics_ItemUnCheck(object sender, EventArgs e)
        {
            //The variable "index" was instantiated previously in the code.
            //get index of checked box and set variable index to that
            index = checkedListBoxBasics.SelectedIndex;

            if (index == 0)
            {
                updateResourcesUncheck("checkedListBoxBasics", 100000, 0, 0, 0, 0, 0, 0, 0);
            }

        }
Isaac
  • 51
  • 1
  • 9
  • 1
    Does this answer your question? [Which CheckedListBox event triggers after a item is checked?](https://stackoverflow.com/questions/3666682/which-checkedlistbox-event-triggers-after-a-item-is-checked) or [this one](https://stackoverflow.com/questions/32291324/manage-checkedlistbox-itemcheck-event-to-run-after-an-item-checked-not-before/32291665). – Trevor Dec 21 '19 at 21:00
  • I'm not sure. When it says that I can check the state of the checkbox, does it mean that I can use an if statement to determine an outcome for the state change of the checkbox for the ItemCheck event and while it is called the ItemCheck event, it is an event that is just triggered whenever an item's check state is changed? – Isaac Dec 21 '19 at 21:36

1 Answers1

0
    private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
    {
        // get the NewValue
        bool isChecked = (e.NewValue == CheckState.Checked);

        // if checked
        if (isChecked)
        {
            // apply event for isChecked
        }
        else
        {
            // apply event for unChecked
        }
    }
  • Where did you get the NewValue property from? – Isaac Dec 21 '19 at 21:46
  • e.NewValue is part of ItemCheckEventArgs. E.CurrentValue is really the old value. –  Dec 22 '19 at 02:42
  • Well, I implemented the code you submitted and it says that e.NewValue is the source of the problem. – Isaac Dec 22 '19 at 05:46
  • 1
    I am not sure I understand what 'it says that e.NewValue is the source of the problem' means. Do you get an error message? –  Dec 22 '19 at 06:56
  • It just underlines it in red and says " 'EventArgs' does not contain a definition for 'NewValue' and no accessible extension method 'NewValue' accepting a first argument of type 'EventArgs' could be found (are you missing a using directive or an assembly reference?)" – Isaac Dec 24 '19 at 20:40