无法检测组合框点击鼠标右键 [英] Unable to detect right mouseclick in ComboBox

查看:106
本文介绍了无法检测组合框点击鼠标右键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ComboBox这是一个简单的下拉风格。我想打开一个新窗口,当用户在列表中的项目用鼠标右击,但我有麻烦它来检测右击已经发生。

I have a ComboBox that is a simple drop down style. I wanted to open a new window when the user right clicks on an item in the list, but am having trouble getting it to detect a right click has occurred.

我的代码

private void cmbCardList_MouseClick(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Right && cmbCardList.SelectedIndex != -1)
    {
        frmViewCard vc = new frmViewCard();
        vc.updateCardDisplay(cmbCardList.SelectedItem);
        vc.Show();
    }
}

如果我改变e.Button == MouseButtons.Left整个事情触发了就好了。任何方式我能得到这个工作,我打算?

If I change e.Button == MouseButtons.Left the whole thing fires off just fine. Any way I can get this working as I intend?

推荐答案

作为墓志铭这个问题,你用正常的可以使这项工作.NET功能;你只需要更深入一点到事件调用堆栈。而不是处理MouseClick事件,处理MouseDown事件。我必须做同样的事情最近,我只是覆盖了onmousedown事件的方法,而不是附加的处理程序。但是,处理程序应该工作了。下面的代码:

As an epitaph to this question, you can make this work using normal .NET functionality; you just have to go a little deeper into the event call stack. Instead of handling the MouseClick event, handle the MouseDown event. I had to do something similar recently, and I simply overrode the OnMouseDown method instead of attaching a handler. But, a handler should work too. Here's the code:

    protected override void OnMouseDown(MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Right && !HandlingRightClick)
        {
            HandlingRightClick = true;
            if (!cmsRightClickMenu.Visible)
                cmsRightClickMenu.Show(this, e.Location);
            else cmsRightClickMenu.Hide();
        }
        base.OnMouseDown(e);
    }

    protected override void OnMouseUp(MouseEventArgs e)
    {
        HandlingRightClick = false;
        base.OnMouseUp(e);
    }

    private bool HandlingRightClick { get; set; }



的HandlingRightClick属性是防止onmousedown事件逻辑的多个触发器;用户界面将发送多的MouseDown消息,可与隐藏右键菜单干扰。为了防止这种情况,我只在第一次的MouseDown触发执行一次逻辑(Logic的足够简单,我不在乎,如果两个调用发生种族,但是可能),那么忽略任何其他的MouseDown触发,直到发生的MouseUp。它并不完美,但是这会做什么,你需要它。

The HandlingRightClick property is to prevent multiple triggers of the OnMouseDown logic; the UI will send multiple MouseDown messages, which can interfere with hiding the right-click menu. To prevent this, I only perform the logic once on the first MouseDown trigger (the logic's simple enough that I don't care if two invocations happen to race, but you might), then ignore any other MouseDown triggers until a MouseUp occurs. It's not perfect, but this'll do what you need it to.

这篇关于无法检测组合框点击鼠标右键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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