Click here to Skip to main content
10,500,000 members (77,560 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: WPF VB.NET
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.

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.
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.
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 26-Aug-14 23:01pm
Edited 28-Aug-14 22:46pm
v2
Comments
Sergey Alexandrovich Kryukov at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You should he able to use the mousedown event[^].
  Permalink  
Comments
Sergey Alexandrovich Kryukov at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 489
1 OriginalGriff 328
2 Mehdi Gholam 145
3 Sascha Lefèvre 135
4 KarstenK 115
0 Sergey Alexandrovich Kryukov 9,308
1 OriginalGriff 8,352
2 Sascha Lefèvre 3,334
3 Maciej Los 2,956
4 Richard Deeming 2,370


Advertise | Privacy | Mobile
Web04 | 2.8.150520.1 | Last Updated 29 Aug 2014
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100