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

Last reviewed: June 21, 1995
Article ID: Q103062
The information in this article applies to:

- Standard and Professional Editions of Microsoft Visual Basic for

  Windows, versions 1.0, 2.0, and 3.0

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:

  • In the MouseDown event, save the X and Y parameters. This position is relative to the upper left corner of the control to drag. Note that the MouseDown event only occurs when DragMode is set to Manual (0).
  • In the DragDrop event, move the control to the position of the mouse pointer adjusted by the position saved in MouseDown.

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 reference words: 1.00 2.00 3.00 runtime run-time
KBCategory: kbprg kbcode
KBSubcategory: PrgCtrlsStd


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: June 21, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.