Click here to Skip to main content
15,885,870 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

How to know if the mouse action is still on click?
I'm about to do a click and hold function. A timer will start when mouse is click on certain position. And when the timer is tick, I want to detect is the mouse is still click then I do run my other code.


EDITED:

This is how I made the code just like your tap + hold on smartphone.

VB
Dim point1, point2 As Point
Dim WithEvents tmrhold As New DispatcherTimer

When button.Click or rectangle.MouseDown, I start the timer and locate the first mouse position.
VB
tmrhold.Interval = TimeSpan.FromMilliseconds(1000)
point1 = Me.PointToScreen(Mouse.GetPosition(Me))
tmrhold.Start()

When timer tick, I check again the current position and compare it with first position. And I make sure the mouse is on click. If I don't do that, when I click and leave the mouse on the same position, it has the same position but not as same action (user still on click). So check if user still on click and position is same.
VB
Private Sub tmrhold_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmrhold.Tick
        tmrhold.Stop()
        If Mouse.LeftButton = MouseAction.LeftClick Then
            point2 = Me.PointToScreen(Mouse.GetPosition(Me))
            If point2 = point1 Then
                'Handle code
            End If
        End If
End Sub
Posted
Updated 28-Aug-14 21:46pm
v2
Comments
Sergey Alexandrovich Kryukov 27-Aug-14 11:17am    
I answered the question by commenting on the Solution 1. Is it clear how can you achieve the functionality you need, with a timer?
—SA

1 solution

You should he able to use the mousedown event[^].
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 27-Aug-14 11:16am    
It actually requires to handle MouseUp as well :-)
Please read the question.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900