如何检测是否将项目添加到 ListBox(或 CheckedListBox)控件 [英] How to detect if items are added to a ListBox (or CheckedListBox) control

查看:10
本文介绍了如何检测是否将项目添加到 ListBox(或 CheckedListBox)控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎是一个基本简单的问题.我有一个带有列表框的 WinForms 对话框.此控件不是通过数据绑定填充的,而是填充了对

This seems like a fundamentally simple question. I have a WinForms dialog box with a listbox. This control is not populated via data-binding but is filled with calls to

listBox.Items.Add (obj);

这个调用可能是从不同的地方异步进行的,我想挂钩列表框并观察其数据成员的变化,以便我可以执行其他 UI 更改(例如启用或禁用与列表框基于列表中的项目数).

It is possible that this call can be made asynchronously from various places and I would like to hook the listbox and watch for changes in its data members so that I can perform other UI changes (such as enable or disable controls which interact with the listbox based on the number of items in the list).

不幸的是,除非我完全一无所知,否则似乎没有可以挂钩的事件或虚拟方法来检测这一点.我可以挂钩选择更改,并且(对于 CheckedListBox)我可以挂钩检查状态更改.但不适用于对基础数据收集的更改.

Unfortunately, unless I'm being completely clueless, there does not seem to be an event or virtual method which can be hooked to detect this. I can hook for select changes and (for CheckedListBox) I can hook for check-state changes. But not for changes to the underlying data collection.

我知道这在 Win32 中是可能的(有一个窗口消息).我错过了什么?

I know this is possible in Win32 (there is a window message for this). What am I missing?

[西蒙编辑]

我被指出了正确的解决方案(我已将其标记为已接受的答案),即覆盖 ListBox 的 WndProc 方法并手动处理列表框消息.这是我确定(并且有效)的解决方案.可以对其进行修改以在事件中提供更多详细信息,或者将消息拆分为单独的事件,但对于我的需要,这已经足够了.

I was pointed to the correct solution (which I have marked as the accepted answer) which is to override the WndProc method of the ListBox and handle the listbox messages manually. Here is the solution that I settled on (and works). It could be modified to provide more details in the event, or split the messages into separate events, but for my needs this is sufficient.

using System;
using System.Windows.Forms;

public class CheckedListBoxEx : CheckedListBox
{
    public CheckedListBoxEx() { }

    private const int LB_ADDSTRING = 0x180;
    private const int LB_INSERTSTRING = 0x181;
    private const int LB_DELETESTRING = 0x182;
    private const int LB_RESETCONTENT = 0x184;

    protected override void WndProc(ref Message m)
    {
        if (m.Msg == LB_ADDSTRING ||
            m.Msg == LB_INSERTSTRING ||
            m.Msg == LB_DELETESTRING ||
            m.Msg == LB_RESETCONTENT)
        {
            ItemsChanged(this, EventArgs.Empty);
        }
        base.WndProc(ref m);
    }

    public event EventHandler ItemsChanged = delegate { };
}

推荐答案

我不知道您可以观看任何事件来显示某个项目已添加到 ListBox.也许您可以改用您描述的 Win32 方法(即抓住一个句柄,使用 WndProc 等).

I don't know of any event that you can watch to show that an item has been added to a ListBox. Perhaps you can use the Win32 method you described instead (i.e. grab a handle, use WndProc, etc.).

或者,也许您可​​以使用另一个添加项目的类.例如,您可以让用户操作在新类中调用 Add 方法,然后将项目添加到 ListBox,而不是直接调用 ListBox 上的 Add 方法.您可以在该类中设置一个事件,让您可以查看添加的内容.

Alternately, perhaps you can use another class that adds items instead. For example, rather than calling the Add method on the ListBox directly, you could have user-actions call the Add method inside the new class which then adds the item to the ListBox. You could set an event inside that class that would allow you to watch what's been added.

我也喜欢另一张海报提到的将 ListBox 子类化的想法......

I also like the idea of subclassing the ListBox as mentioned by another poster....

这篇关于如何检测是否将项目添加到 ListBox(或 CheckedListBox)控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆