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

天大地大妈咪最大 提交于 2019-12-01 17:52:22

问题


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.

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?

The reason I would like to use MouseClick is so that double clicks are automatically recognised.

Thanks for any feedback.


回答1:


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



回答2:


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. Works both, for left and right musebuttons.




回答3:


Apparently the answer to this is that OnClick does not handle right click events for Buttons. The solution was therefore to use MouseUp/MouseDown and check for double clicks/clicks where the mouse moves on/off halfway through manually.



来源:https://stackoverflow.com/questions/8180234/c-sharp-right-mouse-click-on-button-does-not-raise-mouseclick-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!