WinForm ListBox 中的 MouseDown 杀死 SelectedIndexChanged

发布于 2024-11-08 07:26:48 字数 1467 浏览 0 评论 0原文

我正在编写一些代码来检测打开 MultiSelect 的 WindForms ListBox 中的选择切换。由于 SelectedIndexChanged 只能让我看到单击后选择的内容,因此我一直在寻找一种方法来检测单击 ListBox 之前选择的内容。我实现了 MouseDown 事件,并且可以得到我想要的结果,但不幸的副作用是我杀死了 SelectedIndexChanged 事件。它不会着火。

这是已知的行为吗?对于在点击之前进入选择列表有什么想法吗?

谢谢。

编辑以包含请求的代码片段。

设计器生成的事件:

this.lbPhysicianClinic.SelectedIndexChanged += new System.EventHandler( this.lbPhysicianClinic_SelectedIndexChanged );
this.lbPhysicianClinic.MouseDown += new System.Windows.Forms.MouseEventHandler( this.lbPhysicianClinic_MouseDown );

显示 MouseDown 事件的代码片段:

private void lbPhysicianClinic_MouseDown( object sender, MouseEventArgs e )
    {
        List<Clinic_List_ByPhysicianResult> Selected = this.PhysicianGetSelectedClinics( this.lbPhysicianClinic.SelectedIndices );
    }

显示 SelectedIndexChanged 事件的代码片段:

private void lbPhysicianClinic_SelectedIndexChanged( object sender, EventArgs e )
    {
        try
        {
            if ( this.FormInitComplete && this.RefreshUIComplete )
            {
                List<Clinic_List_ByPhysicianResult> Selected = this.PhysicianGetSelectedClinics( this.lbPhysicianClinic.SelectedIndices );

                Clinic_List_ByPhysicianResult DroppedClinic = new Clinic_List_ByPhysicianResult();

我在每个事件中设置了一个断点,如果存在 MouseDown 事件,则 SelectedIndexChanged 事件永远不会触发。它仅在 MouseDown 事件消失时触发。

希望这能澄清一些事情。

I'm writing some code to detect toggling of selections in a WindForms ListBox with MultiSelect turned on. Since SelectedIndexChanged only lets me see what is selected after the click, I was looking for a way to detect what was selected before the ListBox was clicked. I implemented the MouseDown event and I can get exactly what I want, but an unfortunate side effect is that I have killed the SelectedIndexChanged event. It will not fire.

Is this known behavior? Are there any thoughts about getting to the selection list before the click?

Thanks.

Edited to include code snippets as requested.

Designer generated events:

this.lbPhysicianClinic.SelectedIndexChanged += new System.EventHandler( this.lbPhysicianClinic_SelectedIndexChanged );
this.lbPhysicianClinic.MouseDown += new System.Windows.Forms.MouseEventHandler( this.lbPhysicianClinic_MouseDown );

Code snippet showing MouseDown event:

private void lbPhysicianClinic_MouseDown( object sender, MouseEventArgs e )
    {
        List<Clinic_List_ByPhysicianResult> Selected = this.PhysicianGetSelectedClinics( this.lbPhysicianClinic.SelectedIndices );
    }

Code snippet showing SelectedIndexChanged event:

private void lbPhysicianClinic_SelectedIndexChanged( object sender, EventArgs e )
    {
        try
        {
            if ( this.FormInitComplete && this.RefreshUIComplete )
            {
                List<Clinic_List_ByPhysicianResult> Selected = this.PhysicianGetSelectedClinics( this.lbPhysicianClinic.SelectedIndices );

                Clinic_List_ByPhysicianResult DroppedClinic = new Clinic_List_ByPhysicianResult();

I set a breakpoint in each event and if the MouseDown event is there, the SelectedIndexChanged event never fires. It only fires when the MouseDown event is gone.

Hopefully this clarifies things.

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

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

发布评论

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

评论(1

娇纵 2024-11-15 07:26:48

ListBox 在引发 MouseDown 或 SelectedIndexChanged 事件之前更改其选择。

您需要做的是捕获底层 Win32 消息并自行引发事件。您可以子类化 ListBox 来执行此操作。

class MyListBox : ListBox
{
    private const int WM_LBUTTONDOWN = 0x201;

    public event EventHandler PreSelect;

    protected override void WndProc(ref Message m)
    {
        switch (m.Msg)
        {
            case WM_LBUTTONDOWN:
                OnPreSelect();
                break;
        }

        base.WndProc(ref m);
    }

    protected void OnPreSelect()
    {
        if(null!=PreSelect)
            PreSelect(this, new EventArgs());
    }

}

您可以使用 MyListBox 类,并为 PreSelect 事件添加一个处理程序,如下所示:

this.lbPhysicianClinic.PreSelect += 
    new EventHandler(this.lbPhysicianClinic_PreSelect);

在事件处理程序内,您可以在列表框更改所选索引之前访问它们。

The ListBox changes its selection before it raises the MouseDown or SelectedIndexChanged events.

What you need to do is capture the underlying Win32 message and raise an event yourself. You can subclass ListBox to do this.

class MyListBox : ListBox
{
    private const int WM_LBUTTONDOWN = 0x201;

    public event EventHandler PreSelect;

    protected override void WndProc(ref Message m)
    {
        switch (m.Msg)
        {
            case WM_LBUTTONDOWN:
                OnPreSelect();
                break;
        }

        base.WndProc(ref m);
    }

    protected void OnPreSelect()
    {
        if(null!=PreSelect)
            PreSelect(this, new EventArgs());
    }

}

You can use the MyListBox class, and add a handler for the PreSelect event like so:

this.lbPhysicianClinic.PreSelect += 
    new EventHandler(this.lbPhysicianClinic_PreSelect);

Inside the event handler you can access the selected indices before the listbox has changed them.

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