How to drag a control
Posted: Sun Nov 20, 2011 9:53 pm
How do I move a picture box on a double click? In vb 10
Sharing, Teaching and Supporting coders of all ages and levels since 2009
https://www.codenstuff.com/forum/
Dim dClick As Boolean
Dim drag As Boolean
Dim mousex As Integer
Dim mousey As Integer
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
If Not dClick = True Then Exit Sub
drag = True
mousex = Windows.Forms.Cursor.Position.X - PictureBox1.Left
mousey = Windows.Forms.Cursor.Position.Y - PictureBox1.Top
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
If drag Then
PictureBox1.Top = Windows.Forms.Cursor.Position.Y - mousey
PictureBox1.Left = Windows.Forms.Cursor.Position.X - mousex
End If
End Sub
Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
If dClick = True And drag = True Then
drag = False
dClick = False
End If
End Sub
Private Sub PictureBox1_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.DoubleClick
dClick = True
End Sub
Public Class DragControls
Public btf As Boolean = True
Public button As MouseButtons = MouseButtons.Left
Public drag As Boolean = True
Private controlX As Control
Private ptor As Point
Private dragcursor As Boolean = True
Private origCursor As Cursor
Private state As Clicked
Private Enum Clicked
Up
Down
End Enum
Sub SetCtrl(ByVal control As Control)
controlX = control
origCursor = controlX.Cursor
AddHandler controlX.MouseDown, AddressOf MouseDoubleClick
AddHandler controlX.MouseUp, AddressOf MouseUp
AddHandler controlX.MouseMove, AddressOf MouseMove
End Sub
Private Sub MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
If drag Then
If e.Button = button Then
state = Clicked.Down
ptor = e.Location
If dragcursor Then
controlX.Cursor = Cursors.SizeAll
End If
End If
End If
End Sub
Private Sub MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs)
state = Clicked.Up
controlX.Cursor = origCursor
End Sub
Private Sub MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs)
If state = Clicked.Down Then
Dim location As Point
location = controlX.Parent.PointToClient(controlX.MousePosition)
location.X -= ptor.X
location.Y -= ptor.Y
controlX.Location = location
If btf Then
controlX.BringToFront()
End If
End If
End Sub
End Class
Dim dragger as new DragControls
dragger.SetCtrl(picturebox1)