Okay, so just wondering, if its possible. I Have a picturebox (PictureBox1) and just wondering if I dragged the picturebox how could i drag the form?
Printable View
Okay, so just wondering, if its possible. I Have a picturebox (PictureBox1) and just wondering if I dragged the picturebox how could i drag the form?
this works
Code:' Tracks whether the form is in drag mode. If it is, mouse movements
' over the picturebox will be translated into form movements.
Dim Dragging As Boolean
' Stores the offset where the picturebox is clicked.
Dim PointClicked As Point
Private Sub picturebox1_MouseDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) Handles picturebox1.MouseDown
If e.Button = MouseButtons.Left Then
Dragging = True
PointClicked = New Point(e.X, e.Y)
Else
Dragging = False
End If
End Sub
Private Sub picturebox1_MouseMove(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) Handles picturebox1.MouseMove
If Dragging Then
Dim PointMoveTo As Point
' Find the current mouse position in screen coordinates.
PointMoveTo = Me.PointToScreen(New Point(e.X, e.Y))
' Compensate for the position the control was clicked.
PointMoveTo.Offset(-PointClicked.X, -PointClicked.Y - (Me.Height - Me.ClientRectangle.Height))
' Move the form.
Me.Location = PointMoveTo
End If
End Sub
Private Sub picturebox1_MouseUp(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) Handles picturebox1.MouseUp
Dragging = False
End Sub
Thanks! Ill try it now :)
EDIT: Thanks so much Ive been trying to do this for ages!
Hey, thanks .Paul, it worked great for me :-)
Don't forget to mark this thread "resolved".
Thank you Paul.
Smaller/Compressed Code (Easy for memorization):
Code:Dim Moveable As Boolean
Dim LastPos As Point
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
Moveable = True
LastPos = Cursor.Position - Me.Location
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
If Moveable Then
Me.Location = Cursor.Position - LastPos
End If
End Sub
Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
Moveable = False
End Sub