#658 – An Easier Way to Handle Mouse Double Clicks

You can detect mouse double clicks on user interface elements by handling one of the the mouse button events (e.g. MouseDown) on the element and checking to see if the MouseButtonEventArgs.ClickCount property has a value of 2.  The various mouse button events are defined for anything that derives from UIElement or ContentElement.

If your user interface element derives from Control, you can instead just handle the MouseDoubleClick (or PreviewMouseDoubleClick) event.

The sequence of events that fire when the user double-clicks a control with the left mouse button is shown below.  (For simplicity, I’m omitting the MouseLeftButtonDown and MouseLeftButtonUp events and their associated preview events.

  • PreviewMouseDown, with ClickCount = 1
  • MouseDown, with ClickCount = 1
  • PreviewMouseUp
  • MouseUp
  • PreviewMouseDoubleClick
  • PreviewMouseDown, with ClickCount = 2
  • MouseDoubleClick
  • MouseDown, with ClickCount = 2
  • PreviewMouseUp
  • MouseUp

The MouseDoubleClick event does send an instance of MouseButtonEventArgs, but its ClickCount property will always be equal to 1.

Advertisements

#657 – Detecting Double Clicks in User Interface Elements

You can react to a user double-clicking on a user interface element by handling one of the mouse button events and checking the MouseButtonEventArgs.ClickCount field.  When a user double-clicks on an element, all of the mouse down and mouse up events will be fired twice.  During the second round of events, the ClickCount property will have a value of 2.

Here’s an example, where we instrument the MouseDown and MouseUp events for a Label.

When we just click on the element, we see a MouseDown and a MouseUp event, with a ClickCount of 1.

If we double-click on the Label, we’ll see two sets of events.  The second MouseDown event will report a ClickCount of 2.

 

We could distinguish between double-clicking with the left mouse button vs. the right mouse button by handling either the MouseLeftButtonDown and MouseRightButtonDown events.