In this article

How to: Distinguish Between Clicks and Double-Clicks

02/04/2013

10 minutes to read

In this article

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 Click or MouseClick event is performed before the action tied to the DoubleClick or 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 MouseDown event and by using the DoubleClickTime and DoubleClickSize properties of the SystemInformation class. You measure the time between clicks and if a second click occurs before the value of DoubleClickTime is reached and the click is within a rectangle defined by DoubleClickSize, 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 SetStyle 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.

To distinguish between clicks in the MouseDown event

Handle the MouseDown event and determine the location and time span between clicks using the appropriate SystemInformation properties and a Timer component. Perform the appropriate action depending on whether a click or double-click takes place. The following code example demonstrates how this can be done.