I have a CheckListBox in c# and I am trying to trigger an event whenever one of the checkstates in the box is changed. The event purpose is to change some RichTextBox.
I have this piece of code, but it triggers the event only when one of the check boxes is turning from checked to unchecked, for some reason. I tried to figure out what is wrong with my code with no success. Any help will be appreciated.
private void clbAllRooms_ItemCheck(object sender, ItemCheckEventArgs e)
{
//If the checkstate changed, update price
//It updates price only when the state turns from Checked to Uncheck
if (e.NewValue != e.CurrentValue)
Update_rtbPrice();
}
The trouble is no doubt located in your Update_rtbPrice() method. It would have to call the list box' GetItemChecked() method to do something meaningful and that's a problem when you make the method call from the event handler. The item check state doesn't change until after the event runs.
A workaround is to delay the call so it runs after the control's state is updated. Like this: