Understanding Drag and Drop Operations

A woman's hand on a blue mouse

 Burak Karademir / Moment

To "drag and drop" is to hold down the computer mouse button as the mouse is moved, and then release the button to drop the object. Delphi makes it easy to program dragging and dropping into applications.

You can really drag and drop from/to wherever you like, like from one form to another, or from Windows Explorer to your application.

Dragging and Dropping Example

Start up a new project and put one image control on a form. Use Object Inspector to load a picture (Picture property) and then set the DragMode property to dmManual. We'll create a program that will allow moving a TImage control runtime using the drag and drop technique.


Components permit two types of dragging: automatic and manual. Delphi uses the DragMode property to control when the user is able to drag the control. The default value this property is dmManual, which means that dragging components around the application is not allowed, except under special circumstances, for which we have to write the appropriate code. Regardless of the setting for the DragMode property, the component will move only if the correct code is written to reposition it.


The event that recognizes dragging and dropping is called the OnDragDrop event. We use it to specify what we want to happen when the user drops an object. Therefore, if we want to move a component (image) to a new location on a form, we have to write code for the form's OnDragDrop event handler.

The Source parameter of the OnDragDrop event is the object being dropped. The type of the source parameter is TObject. To access its properties, we have to cast it to the correct component type, which in this example is TImage.


We have to use the form's OnDragOver event to signal that the form can accept the TImage control we want to drop on it. Although the Accept parameter defaults to True, if an OnDragOver event handler is not supplied, the control rejects the dragged object (as if the Accept parameter was changed to False).

Run your project, and try dragging and dropping your image. Notice that the image remains visible in its original location while the drag mouse pointer moves. We cannot use the OnDragDrop procedure to make the component invisible while the dragging takes place because this procedure is called only after the user drops the object (if at all).


If you want to change the cursor image presented when the control is being dragged, use the DragCursor property. The possible values for the DragCursor property are the same as those for the Cursor property. You can use animated cursors or whatever you like, like a BMP image file or a CUR cursor file.


If DragMode is dmAutomatic, dragging begins automatically when we press a mouse button with the cursor on the control. If you've left the value of TImage's DragMode property at its default of dmManual, you have to use BeginDrag/EndDrag methods to allow dragging of the component. A more common way to drag and drop is to set DragMode to dmManual and start the dragging by handling mouse-down events.

Now, we'll use the Ctrl+MouseDown keyboard combination to allow dragging to take place. Set TImage's DragMode back to dmManual and write the MouseDown event handler like this:

BeginDrag takes a Boolean parameter. If we pass True (like in this code), dragging begins immediately; if False, it doesn't start until we move the mouse a short distance. Remember that it requires the Ctrl key.

mla apa chicago
Your Citation
Gajic, Zarko. "Understanding Drag and Drop Operations." ThoughtCo, Aug. 27, 2020, thoughtco.com/understanding-drag-and-drop-operations-1058386. Gajic, Zarko. (2020, August 27). Understanding Drag and Drop Operations. Retrieved from https://www.thoughtco.com/understanding-drag-and-drop-operations-1058386 Gajic, Zarko. "Understanding Drag and Drop Operations." ThoughtCo. https://www.thoughtco.com/understanding-drag-and-drop-operations-1058386 (accessed May 31, 2023).