Stonkie
Well-known member
- Joined
- Sep 12, 2007
- Messages
- 279
- Programming Experience
- 1-3
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...
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: