Export (0) Print
Expand All

Control.DragDrop Event

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

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

public:
event DragEventHandler^ DragDrop {
	void add (DragEventHandler^ value);
	void remove (DragEventHandler^ value);
}
/** @event */
public void add_DragDrop (DragEventHandler value)

/** @event */
public void remove_DragDrop (DragEventHandler value)

JScript supports the use of events, but not the declaration of new ones.

The X and Y properties of the DragEventArgs are in screen coordinates, not client coordinates. The following line of Visual C# code converts the properties to a client Point.

Point clientPoint = targetControl.PointToClient(new Point(de.X, de.Y));
NoteNote

In versions earlier than Microsoft .NET Framework version 2.0, if you put a UserControl with DragEnter and DragDrop events on a Windows Form and drag and drop something onto the UserControl at design time, the DropDrop and DropEnter events are raised. However, when you close and reopen the solution, the DragEnter and DragDrop events are not raised again.

For more information about handling events, see Consuming Events.

The following code 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.

The example also demonstrates using custom cursors for the drag-and-drop operation. The example requires 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 UseCustomCursorsCheckCheckBox is checked. The custom cursors are set in the GiveFeedback event handler.

The keyboard state is evaluated in the DragOver event handler for the right ListBox, to determine what the drag operation will be based on 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 a value of None in DragDropEffects. Finally, the status of the drop is displayed in the DropLocationLabelLabel.

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.

This code excerpt demonstrates using the DragDrop event. See the DoDragDrop method for the complete code example.

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( System::String::typeid ) )
   {
      Object^ item = dynamic_cast<Object^>(e->Data->GetData( System::String::typeid ));
      
      // 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";
}

private void listDragTarget_DragDrop(Object sender, 
    System.Windows.Forms.DragEventArgs e)
{
    // Ensure that the list item index is contained in the data.
    if (e.get_Data().GetDataPresent(String.class.ToType())) {
        Object item = (Object)(e.get_Data().GetData(
               String.class.ToType()));
        // Perform drag-and-drop, depending upon the effect.
        if (e.get_Effect().Equals(DragDropEffects.Copy) || 
            e.get_Effect().Equals(DragDropEffects.Move)) {
            // Insert the item.
            if (indexOfItemUnderMouseToDrop != ListBox.NoMatches) {
                listDragTarget.get_Items().Insert(
                    indexOfItemUnderMouseToDrop, item);
            }
            else {
                listDragTarget.get_Items().Add(item);
            }
        }
    }
    // Reset the label text.
    dropLocationLabel.set_Text("None");
} //listDragTarget_DragDrop

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2015 Microsoft