Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WinForms disable double clicks and accept all mouse clicks?

How do you get all clicks to go through the event? I noticed if you click too fast it thinks you are double clicking and doesn't send the clicks to the event handler. Is there a way to get all the clicks?

like image 946
Will Avatar asked Dec 19 '11 12:12

Will


People also ask

How do I turn off double mouse click?

Open the Control Panel. Click or double-click the Mouse or Mouse Settings icon. In the Mouse Properties window, click the Buttons tab, if not already selected. On the Buttons tab, adjust the slider for the Double-click speed option, then click OK.

What is the difference between click and Doubleclick?

Typically, a single click initiates a user interface action and a double-click extends the action. For example, one click usually selects an item, and a double-click edits the selected item.


1 Answers

Not that sure why this question got a bounty, the accepted answer ought to be already pretty close to a solution. Except that you ought to use MouseUp instead of MouseDown, your user typically expects a click action to take effect when he releases the button. Which provides the back-out "oops, didn't mean to click it, move the mouse so it gets ignored" option.

Nevertheless, for the built-in Winforms controls, like PictureBox, this is configurable with the Control.SetStyle() method. Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox:

using System;
using System.Windows.Forms;

class MyPictureBox : PictureBox {
    public MyPictureBox() {
        this.SetStyle(ControlStyles.StandardDoubleClick, false);
    }
}

Do beware however that this won't work for the .NET classes that wrap an existing native Windows control. Like TextBox, ListBox, TreeView, etc. The underlying configuration for that is the WNDCLASSEX.style member, CS_DBLCLKS style flag. The code that sets that style flag is baked into Windows and cannot be changed. You'd need different kind of surgery to turn a double-click back into a single click. You can do so by overriding the WndProc() method, I'll give an example for TextBox:

using System;
using System.Windows.Forms;

class MyTextBox : TextBox {
    protected override void WndProc(ref Message m) {
        // Change WM_LBUTTONDBLCLK to WM_LBUTTONCLICK
        if (m.Msg == 0x203) m.Msg = 0x201;
        base.WndProc(ref m);
    }
}

Just change the class name if you want to do it for other controls. Commandeering Winforms to make it work the way you want never takes much code, just Petzold :)

like image 68
Hans Passant Avatar answered Sep 21 '22 18:09

Hans Passant