简体   繁体   English

C# 鼠标右键单击按钮不会引发 mouseclick 事件

[英]C# Right mouse click on button does not raise mouseclick event

I have a button on a form and want to handle both left and right clicks.我在表单上有一个按钮,想同时处理左右点击。

I am handling the MouseClick event, but this is only raised on a left click.我正在处理 MouseClick 事件,但这仅在左键单击时引发。

Is this a problem somewhere in my code (a setting that I have missed) or the intended functionality?这是我的代码中某处的问题(我错过的设置)还是预期的功能?

If this is not possible to fix, what is the best workaround - to handle the MouseUp event?如果这无法解决,最好的解决方法是什么 - 处理 MouseUp 事件?

The reason I would like to use MouseClick is so that double clicks are automatically recognised.我想使用 MouseClick 的原因是可以自动识别双击。

Thanks for any feedback.感谢您的任何反馈。

Use MouseUp !!使用鼠标向上!

    private void button6_MouseUp(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            MessageBox.Show("LEFT");
        }
        if (e.Button == System.Windows.Forms.MouseButtons.Right)
        {
            MessageBox.Show("RIGHT");
        }
    }

Its hard to answer without code but in general, it should work.没有代码很难回答,但总的来说,它应该可以工作。

 private void Form1_MouseClick(object sender, MouseEventArgs e)
{
  if (e.Button == System.Windows.Forms.MouseButtons.Left)
  {
    MessageBox.Show("LEFT");
  }
  if (e.Button == System.Windows.Forms.MouseButtons.Right)
  {
    MessageBox.Show("RIGHT");
  }
}

// EventHandler // 事件处理器

 this.MouseClick += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseClick);

Edit: There is a MouseDoubleClick Event you might want to use to recognize double clicks.编辑:有一个 MouseDoubleClick 事件,您可能想用它来识别双击。 Works both, for left and right musebuttons.适用于左右鼠标按钮。

Apparently the answer to this is that OnClick does not handle right click events for Button s.显然,对此的答案是OnClick不处理Button的右键单击事件。 The solution was therefore to use MouseUp / MouseDown and check for double clicks/clicks where the mouse moves on/off halfway through manually.因此,解决方案是使用MouseUp / MouseDown并检查鼠标在中途手动打开/关闭的双击/点击。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM