以编程方式更新时防止 UI 控件上的循环事件链

发布于 2024-12-05 09:40:49 字数 190 浏览 1 评论 0原文

我有一个 CheckedListBox,其中包含许多可以单独选中或取消选中的项目。

旁边是代表许多相反项的超集的复选框。这些是三态,对应于其项目的哪些部分被相反地选择。

CheckedListBox 和各个 CheckBox 都订阅检查类型更改事件,这会导致循环引用和溢出。如何以编程方式禁止其中一个在另一个正在更新时接收更改事件?

I have a CheckedListBox which contains many items which can be checked or unchecked individually.

Next to this are CheckBoxes representing supersets of the many items opposite. These are tri-state corresponding to what portion of their items are selected opposite.

Both the CheckedListBox and individual CheckBoxes subscribe to check changed type events which is causing a circular reference and overflow. How can I disable one from receiving change events when the other is updating it, programmatically?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

才能让你更想念 2024-12-12 09:40:49
void checkBox1_N_CheckStateChanged(object sender, EventArgs e) {
    checkedListBox1.ItemCheck -= this.CheckedListBox1_ItemCheck;
    // Handler body where affecting CheckedListBox1.
    checkedListBox1.ItemCheck += this.CheckedListBox1_ItemCheck;
}

最好在对方更新时禁用对方的事件处理程序。

void CheckedListBox1_ItemCheck(object sender, EventArgs e) {
    //Find which 'i' is affected.
    arrOfCBox[i].cb.CheckStateChanged -= arrOfCBox[i].eh;
    // Handler body where affecting checkBox[i].
    arrOfCBox[i].cb.CheckStateChanged += arrOfCBox[i].eh;
}

其中 BoxHandler 至少是:

    class BoxHandler
    {
        public EventHandler eh;
        public CheckBox cb;
    }

类似于 WinForms:暂时禁用事件handler,但令人惊讶的是,它没有得到答案(可能太抽象了?)。

void checkBox1_N_CheckStateChanged(object sender, EventArgs e) {
    checkedListBox1.ItemCheck -= this.CheckedListBox1_ItemCheck;
    // Handler body where affecting CheckedListBox1.
    checkedListBox1.ItemCheck += this.CheckedListBox1_ItemCheck;
}

It works best to disable each others' event handlers as the other is updating.

void CheckedListBox1_ItemCheck(object sender, EventArgs e) {
    //Find which 'i' is affected.
    arrOfCBox[i].cb.CheckStateChanged -= arrOfCBox[i].eh;
    // Handler body where affecting checkBox[i].
    arrOfCBox[i].cb.CheckStateChanged += arrOfCBox[i].eh;
}

where BoxHandler is, at a minimum:

    class BoxHandler
    {
        public EventHandler eh;
        public CheckBox cb;
    }

Similar to WinForms: temporarily disable an event handler, but amazingly that didn't get awarded an answer, (prolly too abstract?).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文