简体   繁体   中英

Double click to Windows form in C#

How can I detect, which mouse button have double clicked the form ie Left, Right or Middle?

Updated: I am using .NET2.0

Have a look at MouseDoubleClick and MouseEventArgs and MouseButtons Enumeration

MouseDoubleClick is one of the Form events.

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

}

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
       }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM