Public Class Form1
#Region " Mouse-Click Count "
''' <summary>
''' The Click-Timer area bounds.
''' </summary>
''' <remarks></remarks>
Private ClickArea As Rectangle
''' <summary>
''' The mouse button clicked.
''' </summary>
''' <remarks></remarks>
Private ClickButton As MouseButtons
''' <summary>
''' Accumulate clicks for the Click-Timer.
''' </summary>
''' <remarks></remarks>
Private ClickCount As Int32
''' <summary>
''' Save the Click-Timer double-click delay time (ms).
''' </summary>
''' <remarks></remarks>
Private ClickDelay As Int32 = SystemInformation.DoubleClickTime
''' <summary>
''' String description of the appropriate owner of the Click-Timer expiry event.
''' </summary>
''' <remarks></remarks>
Private ClickOwner As String = ""
''' <summary>
''' Save the Click-Timer double-click area bounds.
''' </summary>
''' <remarks></remarks>
Private ClickSize As Size = SystemInformation.DoubleClickSize
''' <summary>
''' Create a new Click-Timer with events.
''' </summary>
''' <remarks></remarks>
Private WithEvents ClickTimer As New Timer
''' <summary>
''' Click-Timer "Tick" event handler.
''' </summary>
''' <param name="sender">Event object owner.</param>
''' <param name="e">Event arguments.</param>
''' <remarks></remarks>
Private Sub ClickTimer_TickHandler(ByVal sender As Object, ByVal e As EventArgs) Handles ClickTimer.Tick
Me.ClickTimer.Stop()
Me.ClickCount = 0
End Sub
''' <summary>
''' Initialise the Click-Timer with Owner and valid double-click area.
''' </summary>
''' <param name="aOwnerControl">Click-Timer owner control (string).</param>
''' <param name="aMouseButton">Mouse button clicked.</param>
''' <param name="aClickPoint">Click point for definition of the valid double-click area.</param>
''' <remarks></remarks>
Private Sub ClickTimer_Initialise(ByVal aOwnerControl As String, _
ByVal aMouseButton As MouseButtons, _
ByVal aClickPoint As Point)
' Stop the Click-Timer.
Me.ClickTimer.Stop()
' Save the owner control text.
Me.ClickOwner = aOwnerControl
' Save the mouse button.
Me.ClickButton = aMouseButton
' This is the first click.
Me.ClickCount = 1
' Define the valid double-click area for any multi-clicking.
Me.ClickArea = New Rectangle _
(aClickPoint.X - Me.ClickSize.Width \ 2 _
, aClickPoint.Y - Me.ClickSize.Height \ 2 _
, Me.ClickSize.Width, Me.ClickSize.Height)
' Set the system default double-click delay.
Me.ClickTimer.Interval = Me.ClickDelay
' Start the Click-Timer.
Me.ClickTimer.Start()
End Sub
''' <summary>
''' Register a mouse click (or double click) event.
''' </summary>
''' <param name="aOwnerControl">Click-Timer owner control (string).</param>
''' <param name="aMouseButton">Mouse button clicked.</param>
''' <param name="aClickPoint">Click point for definition of the valid double-click area.</param>
''' <remarks></remarks>
Private Sub ClickTimer_Click(ByVal aOwnerControl As String, _
ByVal aMouseButton As MouseButtons, _
ByVal aClickPoint As Point)
' Handle this click event.
If Me.ClickTimer.Enabled Then
' The Click-Timer is going, stop it and check we haven't changed controls.
Me.ClickTimer.Stop()
If Me.ClickOwner = aOwnerControl _
AndAlso Me.ClickButton = aMouseButton _
AndAlso Me.ClickArea.Contains(aClickPoint) Then
' Working with the same control, same button within a valid double-click area so bump the count.
Me.ClickCount += 1
' Set the system default double-click delay.
Me.ClickTimer.Interval = Me.ClickDelay
' Start the Click-Timer.
Me.ClickTimer.Start()
Else
' Not working with the same control. Initialise the Click-Timer.
Me.ClickTimer_Initialise(aOwnerControl, aMouseButton, aClickPoint)
End If
Else
' The timer is not enabled. Initialise the Click-Timer.
Me.ClickTimer_Initialise(aOwnerControl, aMouseButton, aClickPoint)
End If
End Sub
#End Region
Private Sub TextBox1_Clicked(ByVal sender As Object, ByVal e As MouseEventArgs) _
Handles TextBox1.MouseClick, TextBox1.MouseDoubleClick
Me.ClickTimer_Click(sender.name, e.Button, e.Location)
If ClickCount = 3 Then ' Triple Click to select all text.
sender.SelectAll()
End If
End Sub
End Class