Reputation: 4350
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
Upvotes: 10
Views: 6440
Reputation: 4350
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
Upvotes: 0
Reputation: 103485
That happens throughout Windows. I don't think they added anything special to handle it in .Net.
The normal means of handling this is
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
Upvotes: 13
Reputation: 47873
You could start a timer within the OnMouseClick event handler.
Else
Otherwise
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/
Upvotes: -1
Reputation: 8040
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.
Upvotes: 0