How do i drag labels and/or picture boxes across a form?
Printable View
How do i drag labels and/or picture boxes across a form?
Simply set the picturebox or label's dragmode property to "1-Automatic". Then paste the follow code onto the form:
Code:Private Sub Form_DragDrop(Source As Control, X As Single, Y As Single)
Source.Left = X
Source.Top = Y
End Sub
Good one, Metthew, but I think this would be better:
Code:Private Sub Form_DragDrop(Source As Control, X As Single, Y As Single)
Source.Top = Y - Source.Height / 2
Source.Left = X - Source.Width / 2
End Sub
'Picture box is the the same situtation.
[code]
'Allowing a form control to be movable (DRAG AND DROP)
'...ie drag label1
Option Explicit
Public globalX As Integer
Public globalY As Integer
Private Sub Form_DragDrop(Source As Control, X As _
Single, Y As Single)
Label1.Move X - globalX, Y - globalY
End Sub
Private Sub Label1_MouseDown(Button As Integer, _
Shift As Integer, X As Single, Y As Single)
Label1.Drag vbBeginDrag
globalX = X
globalY = Y
End Sub
[code]
Code:Dim prevX As Long
Dim prevY As Long
Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
prevX = X: prevY = Y
End Sub
Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 Then Picture1.Move Picture1.Left + (X - prevX), Picture1.Top + (Y - prevY)
End Sub