Double click to Windows form in C#
How can I detect, which mouse but开发者_开发问答ton have double clicked the form i.e. Left, Right or Middle?
Updated: I am using .NET2.0
Store the last clicked button in MouseUp event and then check that in the double click event. Sample code:
MouseButtons _lastButtonUp = MouseButtons.None;
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
_lastButtonUp = e.Button;
}
private void Form1_DoubleClick(object sender, EventArgs e)
{
switch (_lastButtonUp)
{
case System.Windows.Forms.MouseButtons.Left:
MessageBox.Show("left double click");
break;
case System.Windows.Forms.MouseButtons.Right:
MessageBox.Show("right double click");
break;
case System.Windows.Forms.MouseButtons.Middle:
MessageBox.Show("middle double click");
break;
}
}
Have a look at MouseDoubleClick and MouseEventArgs and MouseButtons Enumeration
MouseDoubleClick is one of the Form events.
In Whatever_Click or DoubleClick event, you can check the MouseEventArgs e, which contains what key was pressed.
private void Form1_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
Console.WriteLine("Left Mouse Button was clicked!");
else if (e.Button == MouseButtons.Middle)
Console.WriteLine("Middle Mouse Button was clicked!");
}
Other buttons include MouseButtons.Right, MouseButtons.Left
in form_MouseDoubleClick
event you can trace
void Form1_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
// Do Operation
}
}
精彩评论