Enabling Drag-and-Drop in Forms and Controls
All .NET components allowed in a Windows form have a Boolean property named AllowDrop
, which is set to False by default. Turning that value to true is the first and key step to enabling drag-and-drop in a form or a control.
Some drag-and-drop functionality is built into the Control class, which implements very basic functionality required by classes that display information to the user. The Control
class handles user input through the keyboard and mouse. Drag-and-drop results from a combined use of the mouse and keyboard. It is worth noting that in the .NET Framework, the Form classthe base class for all Windows form windowsinherits the Control class. This means that the set of drag-and-drop features is common to all forms and controls.
Accomplishing drag-and-drop operations is done handling a series of events and setting a few properties, most notably the aforementioned AllowDrop
property. Table 1 lists the key members involved in all drag-and-drop operations.
Table 1: These are the properties, methods, and events to build drag-and-drop support in Windows Forms applications.
Boolean property, indicating whether the control can accept data that the user drags onto it. False by default.
Method used to start a drag-and-drop operation.
Event that occurs when a drag-and-drop operation is completed.
Event that occurs when an object is dragged into the control's bounds.
Event that occurs when an object is dragged out of the control's bounds.
Event that occurs when an object is dragged over the control's bounds.
Event that occurs when a drag-and-drop operation is started. The event allows the source of a drag event to modify the appearance of the mouse pointer in order to give the user visual feedback during a drag-and-drop operation.
Event that occurs during a drag-and-drop operation and allows the drag source to determine whether the drag-and-drop operation should be canceled.
property is useful if you want the form or control to accept data from external applications or controls. A form or control that only exports data through drag-and-drop doesn't need to set the value to True