How to Move Controls at Run Time By Using Drag and Drop

ID: Q103062


The information in this article applies to:


SUMMARY

You can move controls at run time by using manual dragging with the Drag method. Automatic dragging (DragMode = 1) does not work well for repositioning controls at run time.


MORE INFORMATION

The key points to remember when using drag and drop to move controls at run time are:

Example Program

The following example program demonstrates how to reposition a picture box at run time. Place the pieces of the program in the appropriate event procedures.

Dim Save_X As Single
Dim Save_Y As Single

' Enter the following Sub as one, single line:

Sub Picture1_MouseDown (Button As Integer, Shift As Integer, X As
   Single, Y As Single)
   Save_X = X       ' save mouse position (relative to this control)
   Save_Y = Y
   Picture1.Drag 1  ' begin dragging
End Sub

' Enter the following Sub as one, single line:

Sub Picture1_MouseUp (Button As Integer, Shift As Integer, X As
   Single, Y As Single)
   Picture1.Drag 2  ' end dragging, do DragDrop event
End Sub

Sub Form_DragDrop (Source As Control, X As Single, Y As Single)
   ' Move the control to the position of the mouse pointer.
   ' Adjust it by the distance the mouse pointer to the upper
   ' left corner of the control.
   Source.Move X - Save_X, Y - Save_Y
End Sub

Sub Picture1_DragDrop (Source As Control, X As Single, Y As Single)
   ' This handles the case when the control is dropped on itself
   ' as would happen if it was only moved a small amount.
   ' This is similar to Form_DragDrop except that the X and Y
   ' parameters are relative to this control, not the form.
   Source.Move Picture1.Left + X - Save_X, Picture1.Top + Y - Save_Y
End Sub 

Additional query words: 1.00 2.00 3.00 runtime run-time


Keywords          : 
Version           : 
Platform          : 
Issue type        : 

Last Reviewed: June 21, 1999