How to Distinguish a DblClick from a Click Event

Last reviewed: June 21, 1995
Article ID: Q109865
The information in this article applies to:

- Standard and Professional Editions of Microsoft Visual Basic

  programming system for Windows, versions 1.0, 2.0, and 3.0

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 reference words: 1.00 2.00 3.00
KBCategory: kbprg kbcode
KBSubcategory: PrgCtrlsStd



THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: June 21, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.