문제

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:

    private void clbAllRooms_ItemCheck(object sender, ItemCheckEventArgs e) {
        this.BeginInvoke(new MethodInvoker(() => Update_rtbPrice()));
    }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top