I want to drag a button from one area of a form to another. I suppose this requires a calculation of the cursor position, and then some code for the Form object. But I cannot find out a way to do this seemingly obvious task. I know to set drag property of my button to "1", but beyond that it is a mystery to me.
Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
OldX = X
OldY = Y
End Sub
Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = vbLeftButton Then
Command1.Left = Command1.Left + (X - OldX)
Command1.Top = Command1.Top + (Y - OldY)
End If
End Sub
The thing I don't like about sebs' code is that when you release the mouse the control centers itself on the mouse cursor. When I move a control, I want it to stay where it is at when I release the mouse.
Originally posted by MarkT
The thing I don't like about sebs' code is that when you release the mouse the control centers itself on the mouse cursor. When I move a control, I want it to stay where it is at when I release the mouse.
I wasn't going to mention that, but as long as you did...
Hey, I was searching for a solution to this problem. Ony I don't use a command control, but I use a shape. And to make it even more dificult, I have set the shape's index to 0.
My app creates square shapes at runtime, how can I move those dynamically created shapes to be move by dragging????
Can anybody help me with this, because the MouseDown etc don't work on containers, or so I think...
Plz, help me out here...
"Experience is something you don't get until just after you need it."
You are right, shapes don't have any events. I think the easiest way to deal with this one is just make your own shape OCX and give it the events you need and use some of the code above.
now what if i have to move two images or buttons one after another. if we use source we can only move one of the image or button and the other one is disabled.
now what if i have to move two images or buttons one after another. if we use source we can only move one of the image or button and the other one is disabled.
if u have another way of doing it do let me know.
Cheers !
Dinesh Manoharan
I don't get what you're trying to do!!
I you want to move one and then move the other one, this
works for me:
VB Code:
Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Command1.Drag 1
End Sub
Private Sub Command2_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Command2.Drag 1
End Sub
Private Sub Form_DragDrop(Source As Control, X As Single, Y As Single)
Private Sub Image1_MouseMove(index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 Then
Image1(index).Left = X + Image1(index).Left
Image1(index).Top = Y + Image1(index).Top
End If
End Sub
Private Sub Pictures_Click()
lclickcount = lclickcount + 1
index = lclickcount - 1
Label1.Caption = lclickcount