Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I differentiate between single and double clicks in .Net?

I want to override OnMouseClick and OnMouseDoubleClick and perform different actions depending on which click style was used.

The problem is that OnMouseClick is happening for both single and double clicks, and is getting called before OnMouseDoubleClick.

I'm certain this must be a common requirement, so I guess I'm missing something pretty obvious. Can someone fill me in?

Edit to add: the MouseEventArgs.Clicks count doesn't help. In the case of a double-click, the first click is handled as a single click in OnMouseClick with MouseEventArgs.Clicks == 1.

Edit to add: the objects are image thumbnails. Single click should toggle selection on and off for export. Double click should make the thumbnail full screen. The selection and "activation" actions are orthogonal. This may indicate an underlying problem with the two actions...

Cheers, Rob

like image 268
Rob Avatar asked Feb 03 '23 13:02

Rob


1 Answers

That happens throughout Windows. I don't think they added anything special to handle it in .Net.

The normal means of handling this is

  • a) just make single click something you'd want to happen before a double click, like select.
  • b) if that's not an option, then on the click event, start a timer. On the timer tick, do the single click action. If the double-click event occurs first, kill the timer, and do the double click action.

The amount of time you set the time for should be equal to the system's Double-Click time (which the user can specify in the control panel). It's available from System.Windows.Forms.SystemInformation.DoubleClickTime. The full details are in the MSDN, here

like image 95
James Curran Avatar answered Feb 06 '23 02:02

James Curran