如何,如果项目被添加到一个ListBox检测(或的CheckedListBox)控制 [英] How to detect if items are added to a ListBox (or CheckedListBox) control

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

问题描述

这似乎是一个从根本上简单的问题。我有一个列表框一个WinForms对话框。这种控制的没有的通过填充数据绑定,但充满调用

  listBox.Items.Add(OBJ);
 

这是可能的,这种电话可以异步从不同的地方进行,我想钩列表框并观察其变化的数据成员,这样我可以执行哪些互动等用户界面的变化(如启用或禁用控制列表框根据列表中的项目)的数量。

不幸的是,除非我是完全无能,似乎没有要,可挂接检测这种事件或虚方法。我可以挂钩的选择和变化(对的CheckedListBox)我可以挂钩的检查状态更改。但不是改变底层的数据采集。

我知道这是可能在Win32中(有此窗口消息)。我在想什么?


[西蒙编辑]

解决方案

我指出了正确的解决方案,(我标记为已接受的答案),这是覆盖ListBox的WndProc方法和手动处理列表框的消息。下面是我看中了(和作品)的解决方案。它可以进行修改,以提供在事件详情或消息分成单独的事件,但我需要,这是不够的。

 使用系统;
使用System.Windows.Forms的;

公共类CheckedListBoxEx:的CheckedListBox
{
    公共CheckedListBoxEx(){}

    私人const int的LB_ADDSTRING =量0x180;
    私人const int的LB_INSERTSTRING = 0x181;
    私人const int的LB_DELETESTRING = 0x182;
    私人const int的LB_RESETCONTENT =量0x184;

    保护覆盖无效的WndProc(参考消息M)
    {
    如果(m.Msg == LB_ADDSTRING ||
    m.Msg == LB_INSERTSTRING ||
    m.Msg == LB_DELETESTRING ||
    m.Msg == LB_RESETCONTENT)
    {
    ItemsChanged(这一点,EventArgs.Empty);
    }
    base.WndProc(REF米);
    }

    公共事件的EventHandler ItemsChanged =委托{};
}
 

解决方案

我不知道,你可以观看到显示项目已被添加到列表框的任何事件。也许你可以使用你所描述,而不是在Win32方法(即抓住把手,使用的WndProc,等等)。

另外,也许你可以使用另一个类,而不是增加项目。例如,而不是直接调用列表框的Add方法,你可以有用户的操作调用新的类,然后添加项目到列表框里面的Add方法。你可以设置在那个类的事件,将让你看什么增加了。

我也很喜欢子类ListBox中所提到的另一个海报的想法......

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);

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).

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.

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


[Edited by Simon]

Solution

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 { };
}

解决方案

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.).

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.

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

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

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