Question

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

Was it helpful?

Solution

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

OTHER TIPS

The issue is that most objects don't implement both. The few objects that can reasonably have different actions for single and double clicks generally are OK to run the single click action before the double click action (highlight then open a folder, enter focus on an address bar before selecting it, etc.) The only way to determine would probably be to wait on the single click for an amount of time and cancel the action for the single click if a double click is detected.

Another possible solution is to have the OnMouseDoubleClick function call OnMouseClick. Since the action in OnMouseClick is a binary toggle, calling it twice resets it to the same state.

So when the user double clicks, windows calls OnMouseClick, then OnMouseDoubleClick. OnMouseDoubleClick calls OnMouseClick (again) to restore the state, then handles the double click.

This feels unsatisfying as a solution, but it works.

Using a timer (to swallow the first click of the double click) is equally unsatisfying, and has the added complication of handling user's double click rate preferences.

Cheers, Rob

You could start a timer within the OnMouseClick event handler.

  • If the timer times out (say 300ms) then a single click is intended.

Else

  • If another OnMouseClick event was generated before the time times out you could examine the x&y position of the click and if its within a particular radius of the first click then action the double click functionality.

Otherwise

  • Handle first click and re-initialise timer for second click

NB: This implementation has the advantage of the fact that both the timeoput and the 'double-click' radius can be configured independantly of the system configuration allowing the s/w to be imported onto multiple machines/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top