Andre D La O
Andre D La O

Reputation: 33

move picture box vb.net mousekeydown and up

i am in visual basic dot net i have a picture box and i need to move it, the user click and move the mouse and the picturebox need move with it when the user release the mouse the picture box have location

Public Class Form1
Dim punto As New Point()

Private Sub PictureBox1_MouseDown1(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
punto = Cursor.Position
PictureBox1.Location = punto
End Sub

Private Sub PictureBox1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
punto = Cursor.Position
PictureBox1.Location = punto

End Sub
End Class 

Upvotes: 0

Views: 8425

Answers (1)

coder
coder

Reputation: 13248

Dim Offset As Point
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseDown
     Offset = New Point(-e.X, -e.Y)
End Sub

Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseMove
     If e.Button = Windows.Forms.MouseButtons.Left Then
         Dim Pos As Point = Me.PointToClient(MousePosition)
         Pos.Offset(Offset.X, Offset.Y)
         PictureBox1.Location = Pos
     End If
    End Sub

Upvotes: 2

Related Questions