Here is the code you are doing.
Public Class Form1 Const WM_LBUTTONDOWN As Integer = &H201 Const WM_LBUTTONDBLCLK As Integer = &H203 Private WithEvents tmrDoubleClicks As Timer Dim isDblClk As Boolean Dim firstClickTime As Date Dim doubleClickInterval As Integer Sub New() ' This call is required by the designer. InitializeComponent() tmrDoubleClicks = New Timer ' Add any initialization after the InitializeComponent() call. tmrDoubleClicks.Interval = 50 doubleClickInterval = CInt(Val(Microsoft.Win32.Registry.CurrentUser. OpenSubKey("Control Panel\Mouse"). GetValue("DoubleClickSpeed", 1000))) End Sub Protected Overrides Sub Dispose(ByVal disposing As Boolean) Try If disposing AndAlso components IsNot Nothing Then components.Dispose() End If If disposing AndAlso tmrDoubleClicks IsNot Nothing Then tmrDoubleClicks.Dispose() End If Finally MyBase.Dispose(disposing) End Try End Sub Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message) Select Case m.Msg Case WM_LBUTTONDOWN If Not isDblClk Then firstClickTime = Now tmrDoubleClicks.Start() End If Case WM_LBUTTONDBLCLK isDblClk = True tmrDoubleClicks.Stop() DoubleClickActivity() isDblClk = False Case Else MyBase.WndProc(m) End Select End Sub Private Sub DoubleClickActivity() 'implement double click activity here Dim r As New Random(Now.TimeOfDay.Seconds) Me.BackColor = Color.FromArgb(r.Next(0, 255), r.Next(0, 255), r.Next(0, 255)) End Sub Private Sub SingleClickActivity() 'implement single click activity here Beep() End Sub Private Sub tmrDoubleClicks_Tick(ByVal sender As Object, ByVal e As System.EventArgs ) Handles tmrDoubleClicks.Tick If Now.Subtract(firstClickTime).TotalMilliseconds > doubleClickInterval Then 'since there was no other click within the doubleclick speed, 'stop waiting and fire the single click activity isDblClk = False tmrDoubleClicks.Stop() SingleClickActivity() End If End Sub End Class
The essence of this code is to delay the click event until the double-click time expires. If during this time another click event occurs, a double-click event is raised without calling the click event. If, however, there is no double-click, the click event is fired.
This delay is especially noticeable on computers with higher double-click speeds. On a typical computer, the double-click speed is 500 ms, so the code fires a click event somewhere between 500 ms and 600 ms after the click occurs.
Alex essilfie
source share