How can I make it so an object is movable by the user? Example: I click a picture box and drag it from one corner to the other. I've been looking up examples for a while and only find file and text drag/drop.
Printable View
How can I make it so an object is movable by the user? Example: I click a picture box and drag it from one corner to the other. I've been looking up examples for a while and only find file and text drag/drop.
You can use the MouseDown and MouseUp events (of the picturebox) to set a flag to true (on MouseDown) and to False (on MouseUp). Then in the MouseMove event, if the flag is True, set the picturebox location to the mouse location.
Ok, I have:
It works, but I see the image everywhere when I'm dragging (In the path I'm dragging), and when I drop it drops way off where I drop it.vb Code:
Private Sub MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) MDown = True End Sub Private Sub MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) If MDown = True Then MyBase.Item(IndexOf(sender)).Location = e.Location End If End Sub Private Sub MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) If MDown = True Then MyBase.Item(IndexOf(sender)).Location = e.Location MDown = False End If End Sub
EDIT: Theres actually 2 paths, and the path that the image is really following is random.
Got it working with:
vb Code:
Private Sub MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) MDown = True Starter = e.Location End Sub Private Sub MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) If MDown = True Then MyBase.Item(IndexOf(sender)).Top = MyBase.Item(IndexOf(sender)).Top + _ (e.Location.Y - Starter.Y) MyBase.Item(IndexOf(sender)).Left = MyBase.Item(IndexOf(sender)).Left + _ (e.Location.X - Starter.X) End If End Sub Private Sub MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) MDown = False End Sub