Identify which mouse button was clicked

Description



This code sample will help you to identify which mouse button was clicked.

Code



Add an event handler on mouse down. For example:



private void CheckMouseClick_MouseDown(object sender, MouseEventArgs e)
{

switch (e.Button)
{
case MouseButtons.Left:
MessageBox.Show("left mouse button" + e.Location.X.ToString() + " " + e.Location.Y.ToString());
break;
case MouseButtons.Right:
MessageBox.Show("right button" + e.Location.X.ToString() + " " + e.Location.Y.ToString());
break;
case MouseButtons.Middle:
MessageBox.Show("middle button" + e.Location.X.ToString() + " " + e.Location.Y.ToString());
break;


}
// switch (e.Location)
}


The above example uses the Button property of MouseEventArgs object to check which button was clicked. The property is a MouseButtons object that can contain various values as shown above.


Comments



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: