Does WinForms disable double clicks and accept all mouse clicks?

How do you get all the clicks for an event? I noticed that if you click too fast, it thinks you double-click and don't send clicks to the event handler. Is there a way to get all the clicks?

+6
source share
3 answers

Not sure why this question was generous, the accepted answer should already be pretty close to a solution. Except that you should use MouseUp instead of MouseDown, your user usually expects the click action to take effect when he releases the button. Which provides a backup of "oops does not mean that you need to click on it, move the mouse so that it is ignored."

However, for built-in Winforms controls, such as PictureBox, this is configured using the Control.SetStyle () method. Add a new class to your project and paste the code shown below. Compilation. Drop the new control on top of the toolbar:

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

However, be careful that this does not work for .NET classes that wrap an existing native Windows control. Like TextBox, ListBox, TreeView, etc. This configuration is based on the WNDCLASSEX.style element, a CS_DBLCLKS style flag. Code that sets the style flag is baked on Windows and cannot be changed. You will need a different type of operation to double-click with one click. You can do this by overriding the WndProc () method, I will give an example for a 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 this for other controls. Commandeering Winforms, to make it work the way you want, never takes up a lot of code, just Petzold :)

+9
source

You want to use MouseDown controls instead of the Click event. MouseDown will be called every time the mouse is "clicked" on this control. A click cannot be called if the system considers it to be a double click. Instead, a DoubleClick event would be created.

+2
source

I think you want to count all the clicks. If you want to count the number of clicks, set one counter variable and increase it in the click event. Maybe this help you ....

-2
source

Source: https://habr.com/ru/post/904075/


All Articles