CheckedListBox's ItemCheck event : bad timing...
I need to produce a list of checked entries from a CheckedListBox whenever an item changes, but that ItemCheck event gets thrown before the item's status is actually changed so the checked items list is incorrect.
I found a forum post telling to use the SelectedIndexChanged event instead, but that won't work if the user checks the item using the space bar.
My two alternatives are to :
1. Use the ItemCheckEventArgs to figure out what item changed and how. Since the code that gets the list of checked items is a few methods down on the calls hierachy, I would need to pass the event args around (or more likely an homemade representation of its data for consistency).
2. Use a class member boolean to see if I'm in the ItemCheck event handler and set the item's checked state to its correct value before calling the method that does the processing. The boolean is used to avoid an infinite call of the event handler, it would just return without any processing in the case the event handler is in the call stack itself.
Anyone knows how to write this in a cleaner fashion?
Btw, I also tought about inheriting the CheckedListBox control to override the SetCheckedState method, but it is not virtual...
Last edited by Stonkie; 05-12-2008 at 10:05 AM.
The human mind's capability to comprehend abstract concepts is limited to the vocabulary it can use to describe it. The more precise the building blocks, the more complex the thoughts that can emerge... Sounds like the evolution of programming languages doesn't it?