Export (0) Print
Expand All

Control.DragDrop Event

Occurs when a drag-and-drop operation is completed.

[Visual Basic]
Public Event DragDrop As DragEventHandler
[C#]
public event DragEventHandler DragDrop;
[C++]
public: __event DragEventHandler* DragDrop;

[JScript] In JScript, you can handle the events defined by a class, but you cannot define your own.

Event Data

The event handler receives an argument of type DragEventArgs containing data related to this event. The following DragEventArgs properties provide information specific to this event.

Property Description
AllowedEffect Gets which drag-and-drop operations are allowed by the originator (or source) of the drag event.
Data Gets the IDataObject that contains the data associated with this event.
Effect Gets or sets the target drop effect in a drag-and-drop operation.
KeyState Gets the current state of the SHIFT, CTRL, and ALT keys, as well as the state of the mouse buttons.
X Gets the x-coordinate of the mouse pointer, in screen coordinates.
Y Gets the y-coordinate of the mouse pointer, in screen coordinates.

Remarks

Note   The X and Y properties of the DragEventArgs are in screen coordinates, not client coordinates. The following line of C# code converts the properties to a client Point:
Point clientPoint = targetControl.PointToClient(new Point(de.X, de.Y));

For more information about handling events, see Consuming Events.

Example

[Visual Basic, C#, C++] The following example demonstrates a drag-and-drop operation between two ListBox controls. The example calls the DoDragDrop method when the drag action starts. The drag action starts if the mouse has moved more than SystemInformation.DragSize from the mouse location during the MouseDown event. The IndexFromPoint method is used to determine the index of the item to drag during the MouseDown event.

[Visual Basic, C#, C++] The example also demonstrates using custom cursors for the drag-and-drop operation. The example assumes that two cursor files, 3dwarro.cur and 3dwno.cur, exist in the application directory, for the custom drag and no-drop cursors, respectively. The custom cursors will be used if the UseCustomCursorsCheck CheckBox is checked. The custom cursors are set in the GiveFeedback event handler.

[Visual Basic, C#, C++] The keyboard state is evaluated in the DragOver event handler for the right ListBox, to determine what the drag operation will be based upon state of the SHIFT, CTRL, ALT, or CTRL+ALT keys. The location in the ListBox where the drop would occur is also determined during the DragOver event. If the data to drop is not a String, then the DragEventArgs.Effect is set to DragDropEffects.None. Finally, the status of the drop is displayed in the DropLocationLabel Label.

[Visual Basic, C#, C++] The data to drop for the right ListBox is determined in the DragDrop event handler and the String value is added at the appropriate place in the ListBox. If the drag operation moves outside the bounds of the form, then the drag-and-drop operation is canceled in the QueryContinueDrag event handler.

[Visual Basic, C#, C++] This code excerpt demonstrates using the DragDrop event. See the DoDragDrop method for the complete code example.

[Visual Basic] 
Private Sub ListDragTarget_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs) Handles ListDragTarget.DragDrop
    ' Ensures that the list item index is contained in the data.

    If (e.Data.GetDataPresent(GetType(System.String))) Then

        Dim item As Object = CType(e.Data.GetData(GetType(System.String)), System.Object)

        ' Perform drag and drop, depending upon the effect.
        If (e.Effect = DragDropEffects.Copy Or _
            e.Effect = DragDropEffects.Move) Then

            ' Insert the item.
            If (indexOfItemUnderMouseToDrop <> ListBox.NoMatches) Then
                ListDragTarget.Items.Insert(indexOfItemUnderMouseToDrop, item)
            Else
                ListDragTarget.Items.Add(item)

            End If
        End If
        ' Reset the label text.
        DropLocationLabel.Text = "None"
    End If
End Sub

[C#] 
private void ListDragTarget_DragDrop(object sender, System.Windows.Forms.DragEventArgs e) 
{
    // Ensure that the list item index is contained in the data.
    if (e.Data.GetDataPresent(typeof(System.String))) {

        Object item = (object)e.Data.GetData(typeof(System.String));

        // Perform drag and drop, depending upon the effect.
        if (e.Effect == DragDropEffects.Copy ||
            e.Effect == DragDropEffects.Move) {
        
            // Insert the item.
            if (indexOfItemUnderMouseToDrop != ListBox.NoMatches)
                ListDragTarget.Items.Insert(indexOfItemUnderMouseToDrop, item);
            else
                ListDragTarget.Items.Add(item);
            
        }
    }
    // Reset the label text.
    DropLocationLabel.Text = "None";
}

[C++] 
private:
   void ListDragTarget_DragDrop(Object* /*sender*/, 
      System::Windows::Forms::DragEventArgs* e) {
         // Ensure that the list item index is contained in the data.
         if (e->Data->GetDataPresent(__typeof(System::String))) {
            Object* item = dynamic_cast<Object*>(
               e->Data->GetData(__typeof(System::String)));

            // Perform drag and drop, depending upon the effect.
            if (e->Effect == DragDropEffects::Copy ||
               e->Effect == DragDropEffects::Move)
            {

               // Insert the item.
               if (indexOfItemUnderMouseToDrop != ListBox::NoMatches)
                  ListDragTarget->Items->Insert(
                  indexOfItemUnderMouseToDrop, item);
               else
                  ListDragTarget->Items->Add(item);

            }
         }
         // Reset the label text.
         DropLocationLabel->Text = S"None";
      }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

Control Class | Control Members | System.Windows.Forms Namespace | OnDragDrop

Show:
© 2014 Microsoft