Wednesday, April 28, 2010

Code: Handling Back, Forward, and Middle Mouse Button Clicks

I’m currently studying to become a MCPD in Windows Forms development.  I don’t do very much development in windows forms, so some of what I’m learning is pretty new.  I learned today that .Net Windows Forms can easily handle mouse clicks that come from the back and forward buttons as well as the middle mouse button.  To do this you just need to handle the MouseClick event of a control as shown in the code below.

private void Form1_MouseClick(object sender, MouseEventArgs e)
{
switch (e.Button)
{
case System.Windows.Forms.MouseButtons.XButton1:                   
case System.Windows.Forms.MouseButtons.XButton2:
case  System.Windows.Forms.MouseButtons.Middle:
MessageBox.Show(e.Button.ToString());
break;
}
}