Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Click or DoubleClick

0.00/5 (No votes)
24 Sep 2013 1  
This code processes a Click or a DoubleClick event.

Introduction

I wanted to have a control that will process a Click event or a DoubleClick event.  All the solutions I could find on the internet used a rather complicated rollback process.  This is a very simple solution that could be understood by just about anybody.

Using the code

The form uses a label for the control. Just Click or DoubleClick on the label to try it out.  The code has a constant to enable the DoubleClick speed to be altered. 

Public Class frmClickDoubleClick

  Const DOUBLE_CLICK_SPEED As Integer = 250   'mouse DoubleClick speed
  Dim isDoubleTriggered As Boolean = False

  Private Sub labLabel_Click(ByVal sender As Object, _
          ByVal e As System.EventArgs) Handles labLabel.Click
    'delay (250ms) to make sure the DoubleClick code triggers first
    System.Threading.Thread.Sleep(DOUBLE_CLICK_SPEED)
    System.Windows.Forms.Application.DoEvents()

    If isDoubleTriggered Then
    'if DoubleClick event triggered then supress the SingleClick code
      isDoubleTriggered = False
      'reset the DoubleClick flag
      Exit Sub
    End If

    Me.BackColor = Color.Coral              'SingleClick code
    Me.Text = "Single Click"
  End Sub

  Private Sub labLabel_DoubleClick(ByVal sender As Object, _
              ByVal e As System.EventArgs) Handles labLabel.DoubleClick
    isDoubleTriggered = True                   'flag the DoubleClick event
    Me.BackColor = Color.Aquamarine            'DoubleClick code
    Me.Text = "Double Click"
  End Sub

End Class 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here