0

我在 c# 中有一个 CheckListBox,并且每当更改框中的一个检查状态时,我都会尝试触发一个事件。该事件的目的是改变一些RichTextBox。

我有这段代码,但仅当其中一个复选框出于某种原因从选中变为未选中时才会触发事件。我试图弄清楚我的代码有什么问题但没有成功。任何帮助将不胜感激。

    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();
    }
4

1 回答 1

1

毫无疑问,问题出在您的 Update_rtbPrice() 方法中。它必须调用列表框的 GetItemChecked() 方法来做一些有意义的事情,当您从事件处理程序进行方法调用时,这是一个问题。直到事件运行,项目检查状态才会改变。

一种解决方法是延迟调用,使其在控件状态更新后运行。像这样:

    private void clbAllRooms_ItemCheck(object sender, ItemCheckEventArgs e) {
        this.BeginInvoke(new MethodInvoker(() => Update_rtbPrice()));
    }
于 2012-01-16T18:00:42.857 回答