Skip to content

Latest commit

 

History

History
42 lines (30 loc) · 4.02 KB

how-to-distinguish-between-clicks-and-double-clicks.md

File metadata and controls

42 lines (30 loc) · 4.02 KB
title descriptions ms.date dev_langs helpviewer_keywords ms.assetid
How to: Distinguish Between Clicks and Double-Clicks
Learn how to distinguish between clicks and double-clicks so that you can program double-clicks to extend the action of clicks.
03/30/2017
csharp
vb
cpp
mouse [Windows Forms], click
mouse [Windows Forms], double-click
mouse clicks [Windows Forms], single versus double
d836ac8c-85bc-4f3a-a761-8aee03dc682c

How to: Distinguish Between Clicks and Double-Clicks

Typically, a single click initiates a user interface (UI) action and a double-click extends the action. For example, one click usually selects an item, and a double-click edits the selected item. However, the Windows Forms click events do not easily accommodate a scenario where a click and a double-click perform incompatible actions, because an action tied to the xref:System.Windows.Forms.Control.Click or xref:System.Windows.Forms.Control.MouseClick event is performed before the action tied to the xref:System.Windows.Forms.Control.DoubleClick or xref:System.Windows.Forms.Control.MouseDoubleClick event. This topic demonstrates two solutions to this problem. One solution is to handle the double-click event and roll back the actions in the handling of the click event. In rare situations you may need to simulate click and double-click behavior by handling the xref:System.Windows.Forms.Control.MouseDown event and by using the xref:System.Windows.Forms.SystemInformation.DoubleClickTime%2A and xref:System.Windows.Forms.SystemInformation.DoubleClickSize%2A properties of the xref:System.Windows.Forms.SystemInformation class. You measure the time between clicks and if a second click occurs before the value of xref:System.Windows.Forms.SystemInformation.DoubleClickTime%2A is reached and the click is within a rectangle defined by xref:System.Windows.Forms.SystemInformation.DoubleClickSize%2A, perform the double-click action; otherwise, perform the click action.

To roll back a click action

  • Ensure that the control you are working with has standard double-click behavior. If not, enable the control with the xref:System.Windows.Forms.Control.SetStyle%2A method. Handle the double-click event and roll back the click action as well as the double-click action. The following code example demonstrates a how to create a custom button with double-click enabled, as well as how to roll back the click action in the double-click event handling code.

    [!code-csharpSystem.Windows.Forms.ButtonDoubleClick#1] [!code-vbSystem.Windows.Forms.ButtonDoubleClick#1]

To distinguish between clicks in the MouseDown event

Compiling the Code

These examples require:

  • References to the System, System.Drawing, and System.Windows.Forms assemblies.

See also