Knowledge Base |

# How to Distinguish a DblClick from a Click Event

Article ID: 109865

Article Last Modified on 12/9/2003

APPLIES TO

- Microsoft Visual Basic 1.0 Standard Edition
- Microsoft Visual Basic 2.0 Standard Edition
- Microsoft Visual Basic 3.0 Professional Edition
- Microsoft Visual Basic 2.0 Professional Edition
- Microsoft Visual Basic 3.0 Professional Edition

This article was previously published under Q109865

## SUMMARY

Usually when you issue two consecutive mouse clicks on a form or object, you will receive a click event for the first mouse click and another click event or a double-click event for the second mouse click -- depending on the period of time between the mouse clicks.
At times, you may want to receive only the double-click event without the preceding click event. This article describes how to write code to accomplish this.

## MORE INFORMATION

The following example demonstrates how to receive only a DblClick event on a form rather than a Click and then a DblClick event.

### Step-by-Step Example

1. Start a new project in Visual Basic. Form1 is created by default.
2. Add a Timer control (Timer1) to Form1.
3. Add the following code to the (general) (declarations) section of Form1:

          ' The following Function gets the DoubleClickSpeed from the WIN.INI
          ' file. Windows uses this setting to determine how close together two
          ' consecutive mouse clicks must occur for it to be interpreted as
          ' a double-click.
          ' Enter the following Declare statement on one, single line:
          Declare Function GetProfileInt% Lib "Kernel" (ByVal lpAppName$,
             ByVal lpKeyName$, ByVal nDefault%)

4. Add the following code to the specified event procedures:

          Sub Form_Load ()
             clickSpeed% = GetProfileInt("Windows", "DoubleClickSpeed", 0)
             Timer1.Enabled = False         ' Timer should be off to begin with.
             Timer1.Interval = clickSpeed%  ' After the timer is turned on
                                            ' it will trigger the Timer Event
                                            ' after a specific amount of time
                                            ' equal to that of DoubleClickSpeed.
          End Sub

          Sub Form_Click ()
             Timer1.Enabled = True          ' Turn the timer on. If another mouse
                                            ' click does not occur within the
                                            ' DoubleClickSpeed interval, the
                                            ' Timer1_Timer Event will fire.
          End Sub

          Sub Form_DblClick ()
             Timer1.Enabled = False         ' Turn off the timer. This
                                            ' prevents the Timer1_Timer event
                                            ' from firing and thus the code
                                            ' for a single click will not be
                                            ' processed.
             Print "This is a double-click" ' Code for double-click goes here.
          End Sub

          Sub Timer1_Timer ()
             ' If this event occurs then there has not been another mouse
             ' click since the previous one within the DoubleClickSpeed
             ' time interval. Thus there will be two Click events rather
             ' than a DblClick Event.
             Timer1.Enabled = False         ' Turn off the timer so the
                                            ' Timer1_Timer Event does not
                                            ' continue to fire.
             Print "This is a Single Click" ' Code for single click goes here.
          End Sub

5. Run the program.
6. Click once, wait a while, click again to receive two single clicks.
7. Click twice quickly to receive a double-click with no preceding single click.

NOTE: The single click code will not occur until the DoubleClickSpeed interval passes and the Timer1_Timer event is fired.

Additional query words: 1.00 2.00 3.00

Keywords: KB109865