The goal is to pan a form or image on the form when left clicking, and dragging the mouse. The below code works perfectly fine for my needs, but there is only one issue. When I left click and drag the mouse, the form pans quicker than the mouse, making it awkward. Is there any method I can use to make the form pan at the same rate as my mouse? Here is my code:
Private leftClick as Point
Private Sub Form_OnMouseDown(sender as Object, e as MouseEventArgs) Handles Form.MouseDown
leftClick = e.Position
End Sub
Private Sub Form_OnMouseMove(sender as Object, e as MouseEventArgs) Handles Form.MouseMove
'Resolves issue where mouse keeps on moving even if not
Static MyPoint As New Point
If e.Location = MyPoint Then Exit Sub
MyPoint = e.Location
'If we left click anywhere on form, pan the form
If e.Button = Windows.Forms.MouseButtons.Left Then
Dim DeltaX As Integer = (leftClick.X - e.x)
Dim DeltaY As Integer = (leftClick.Y - e.y)
Dim newX as Integer = DeltaX - Me.AutoScrollPosition.X
Dim newY as Integer = DeltaY - Me.AutoScrollPosition.Y
'Then we set the new autoscroll position.
Me.AutoScrollPosition = New Point(newX, newY)
End If
End Sub
Thanks!
You have a little mistake in your code:
Private Sub Form_OnMouseMove(sender as Object, e as MouseEventArgs) Handles Form.MouseMove
'Resolves issue where mouse keeps on moving even if not
Static MyPoint As New Point
If e.Location = MyPoint Then Exit Sub
MyPoint = e.Location
'If we left click anywhere on form, pan the form
If e.Button = Windows.Forms.MouseButtons.Left Then
Dim DeltaX As Integer = (leftClick.X - e.x)
Dim DeltaY As Integer = (leftClick.Y - e.y)
Dim newX as Integer = DeltaX - Me.AutoScrollPosition.X
Dim newY as Integer = DeltaY - Me.AutoScrollPosition.Y
'Then we set the new autoscroll position.
Me.AutoScrollPosition = New Point(newX, newY)
'Add this code
leftClick.X = e.X '<---
leftClick.Y = e.Y '<---
End If
End Sub
You need the difference between the previous mouse position and the current one. What you did was to calculate the total from the position you clicked.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With