In a Windows Forms application, a drag-and-drop functionality refers to the transferring of data with the mouse. It enables a user to drag data such as a text, an image, or other object with the mouse’s left-button from one control and drop it to another control. This functionality is an event-driven process that occurs when the data is copied from the source control and when the data is dropped onto the target control. The source as well as target control events are involved in implementing the drag-and-drop operations are as follows:

  • Source Control Events: These events occur when a mouse pointer triggers some action on a source control. The following are the source control events:

    . To get a glimpse of these PrepKits, you can download its free demo version (which contains 15 free practice questions) from:

    Download link: http://www.ucertify.com/exams/Microsoft/70-526-VB.html

    1. MouseDown Event: It occurs when the mouse button is pressed and the mouse pointer is over the source control. A method that handles this event calls the DoDragDrop method of the source control.
    2. GiveFeedback Event: It occurs when a drag-and-drop operation is initiated. The appearance of the mouse pointer can be modified by the source of a drag event. In this event, a user can obtain the visual feedback during the operation. This event provides a custom mouse pointer to a user.
    3. QueryContinueDrag Event: It occurs during a drag-and-drop operation. It is raised when the keyboard or the mouse button state changes during the operation. It also enables the drag source in order to determine whether the drag-and-drop operation is cancelled.
  • Target Control Events: These events occur when a mouse pointer triggers some action on a target control. The following are the source control events:
    1. DragEnter Event: It occurs when an object is dragged into the bounds of the target control. It is raised when the mouse cursor is first dragged over the target control during a drag-and-drop operation.
    2. DragOver Event: It occurs when an object is dragged over the bounds of the target control. It is raised when the mouse cursor is within the bounds of the control.
    3. DragDrop Event: It occurs when the mouse button is released over the target control and a drag-and-drop operation is completed.
    4. DragLeave Event: It occurs when the an object is dragged out of the bounds of the target control. It occurs when the current drag-and-drop operation is cancelled.

Sequence of a Drag-and-Drop Operation

The following is the general sequence of events that take place in a drag-and-drop operation:

  1. The drag-and-drop operation is initiated by calling the DoDragDrop method within the MouseDown event handler on the source control. The method contains two parameters, namely the Object data that specifies the desired data to pass and the DragDropEffects data value that specifies a particular drag operation on a control. The method determines under which control the current mouse cursor is located. It then checks whether the valid drop target operation has been performed. In this sequence, a new DataObject object is automatically created.
  2. The GiveFeedback event is raised when a custom mouse pointer is displayed during the dragging operation. The QueryContinueDrag event is raised to determine whether the drag operation should be continued or aborted.
  3. The drop-and-drop operation is allowed when the AllowDrop property is set to true in the Properties window at design time. The property can be set to true programmatically in the Form_Load event of a Windows form.
  4. The DragEnter event for a target control is raised when the mouse cursor is first dragged over the control. The DragOver event takes place when a mouse pointer stays over the bounds of a control.
  5. Finally, the DragDrop event is raised when the mouse button is released over a valid target control. In the Windows form, the event handler extracts the appropriate data from the DataObject object. Data is then displayed in the target control.

DragDropEffects Enumeration Values

The DragDropEffects enumeration identifies the effects of a drag-and-drop operation in a Windows Forms application. It contains the FlagsAttribute attribute that enables a user to perform a bitwise combination of its member values. It is used by the DragEventArgs, GiveFeedbackEventArgs, and Control classes. The following are the member values that can exist in this enumeration:

Member Values Description
All During a drag-and-drop operation data is copied, dragged, and removed from the source control. The data is then scrolled and dropped onto the target control.
Copy During a drag operation, data is copied to the target control.
Link During the operation, data from the source control is linked to the target control.
Move During the operation, data is moved from the source control to the target control.
None During a drag-and-drop operation, the target control does not accept the data from the source control.
Scroll During a drag-and-drop operation, the scrolling operation is initiated or is currently being performed.

Implementing Drag-and-Drop Operations between Applications

Generally, a drag-and-drop operation is performed within a single Windows Forms application. This operation can also be implemented between two applications. The following conditions should be fulfilled in order to enable a drag-and-drop operation between applications:

  • A target control should allow one of the drag effects being specified when the DoDragDrop method is called.
  • A target control should accept data in the same format that was set when the DoDragDrop method is called.

Implementing Drag-and-Drop Operations by using a TreeView Control

The implementation of a drag-and-drop operation in a TreeView control is slightly different from other controls. When a drag operation is initiated on a TreeView node, the ItemDrag event is raised. The event passes an instance of ItemDragEventArgs to a method that handles this event. The ItemDragEventArgs object contains a reference that is copied to the DataObject object in the DoDragDrop method.

Download link: http://www.ucertify.com/exams/Microsoft/70-526-VB.html