How can i drag something to somewhere else on the form?
Printable View
How can i drag something to somewhere else on the form?
set the DragMode property on the controls you want to be able to drag and drop to "Automatic," and then for the item that you want to be able to "drag onto," insert something like this into the "DragDrop" or "DragOver" events:
For the code above, I made a CommandButton have a DragMode of Automatic, and when I ran it, if I dragged the button somewhere on the form its position would move. Very basic example, but it can get you started I suppose!VB Code:
Private Sub Form_DragDrop(Source As Control, X As Single, Y As Single) Source.Left = X Source.Top = Y End Sub
yeah, i done that with my picture (it was a picture i used btw) and it worked and moved for me but then it jumped places a bit and i've found out that's cause my mouse wasn't in the very top corner. How do i make it so it doesn't jump down?
can someone help please? and btw, it was an imagebox called "house", not a picture.
Save the mouse coordinates (MouseDown event) when the drag operation begins. Adjust the X, Y coordinates of the DragDrop event.
You will need to set the DragMode property back to Manual as the Mouse events are not fired when DragMode is Automatic.
The following works fine if the Image control is on the Form and you are dropping it on the Form.
VB Code:
Private sngDragX As Single Private sngDragY As Single Private Sub Form_DragDrop(Source As Control, X As Single, Y As Single) Source.Move X - sngDragX, Y - sngDragY End Sub Private Sub Image1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) sngDragX = X: sngDragY = Y Image1.Drag vbBeginDrag End Sub