Microsoft KB Archive/103062

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

Article ID: 103062

Article Last Modified on 12/9/2003

-

APPLIES TO


 * Microsoft Visual Basic 1.0 Standard Edition
 * Microsoft Visual Basic 2.0 Standard Edition
 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 2.0 Professional Edition
 * Microsoft Visual Basic 3.0 Professional Edition

-



This article was previously published under Q103062



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 query words: 1.00 2.00 3.00 runtime run-time

Keywords: KB103062

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.