How to get a right click mouse event? Changing EventArgs to MouseEventArgs causes an error in Form1Designer?

后端 未结 7 1833
-上瘾入骨i
-上瘾入骨i 2020-12-03 09:37

I have a method to detect the left click event that visual studio made by double clicking on the form.

private void pictureBox1_Click(object sender, EventArg         


        
7条回答
  •  清歌不尽
    2020-12-03 10:31

    For me neither the MouseClick or Click event worked, because the events, simply, are not called when you right click. The quick way to do it is:

     private void button1_MouseUp(object sender, MouseEventArgs e)
     {
            if (e.Button == MouseButtons.Right)
            {
                //do something here
            }
            else//left or middle click
            {
                //do something here
            }
     }
    

    You can modify that to do exactly what you want depended on the arguments' values.

    WARNING: There is one catch with only using the mouse up event. if you mousedown on the control and then you move the cursor out of the control to release it, you still get the event fired. In order to avoid that, you should also make sure that the mouse up occurs within the control in the event handler. Checking whether the mouse cursor coordinates are within the control's rectangle before you check the buttons will do it properly.

提交回复
热议问题