Partager via


Allow Users to Drag and Drop Controls Sample

File: ...\Samples\Solution\Forms\Ddrop.scx

This sample explains how to implement drag and drop operations. One page on the form illustrates manual dragging of controls by calling the Drag method. Explicitly calling the Drag method makes it possible for you to process other code in the Click event of the control. The other page illustrates using the Automatic DragMode setting so that the Drag and Drop operation is started automatically when the user presses the mouse button over the controls.

Repositioning the Command Button

In the MouseMove event of cmdDrop, if the left mouse button is down, find out how far the mouse pointer is from the top and the left of the command button and begin the drag operation.

LPARAMETERS nButton, nShift, nXCoord, nYCoord
IF nButton = 1 && Left button
   THISFORM.XOffset = nXCoord – THIS.Left
   THISFORM.YOffset = nYCoord - THIS.Top
   THIS.Drag
ENDIF

In the DragDrop event of the page that the command button is on, reposition the command button.

oSource.Left = nXCoord - THISFORM.XOffset
oSource.Top = nYCoord - THISFORM.Yoffset

Changing the Color of the Shape

The DragMode property of the colored squares on the Change Colors page of the form is set to 1 –Automatic. As soon as the user presses the mouse button on the squares, the drag operation begins. Any code added to the Click event of the squares would never be executed.

Code in the DragDrop event of the circle changes its BackColor property to match that of the drag source.

LPARAMETERS oSource, nXCoord, nYCoord
THIS.BackColor = oSource.BackColor

See Also

Allow Users to Choose List Values | Solutions Samples | OLE Drag-and-Drop Data Formats