Verified list item Verification event Event Strange behavior

private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e) { if (checkedListBox1.GetItemChecked(i) == false) { ... } else { ... } } 

For some reason, when the code above is executed, it does the opposite of what I would like to do. When an element is checked for the first time, it does nothing, however, when it is not set, it does what is in the else statement (again, opposite to what it should do). Is there any property that I forget about here?

+4
source share
2 answers

You should use e.NewValue instead of checkedListBox1.GetItemChecked(i) . The reason is that checkedListBox1.GetItemChecked is a cached state because the ItemCheck event occurs before the internal value is updated.

This will work as you expect:

 private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e) { if (e.NewValue == CheckState.Checked) { ... } else { ... } } 

Secondly, about why it does not respond the first time a flag is clicked: because the CheckedListBox object requires an element that will be selected before changing the value of the flag with mouse clicks.

To achieve a similar effect, set checkedListBox1.CheckOnClick = true . This will cause the flag to be checked each time you click on the flag or the list item itself.

+4
source

MSDN indicates that the validation status is not updated in the ItemCheck event until it ends. You are probably looking for e.NewValue .

0
source

All Articles