Question

Here is my code:

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

I am able to do what the user wants, (drag picture & add button) although..
Problem 1
Whenever I drag the pictureBox inside the panel, it somewhat offsets a little to the right wherever I bring it. Technically, as I move the mouse, the picturebox just moves to the right before following the sequence of the mouse, I know its just minor, but its a bug that I can't let the users to see it.

Hope you guys can help! thank you!

Was it helpful?

Solution

You need to store the original client location of the picturebox (on mouse down) and do the offset on this point:

Private offset As Point
Private pbpos As Point

Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseDown
    Me.pbpos = Me.PictureBox1.Location
    Me.offset = Control.MousePosition
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
        Me.PictureBox1.Location = New Point((Me.pbpos.X + (Control.MousePosition.X - Me.offset.X)), (Me.pbpos.Y + (Control.MousePosition.Y - Me.offset.Y)))
    End If
End Sub
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top