Export (0) Print
Expand All

Performing Drag-and-Drop Operations in Windows Forms

Visual Studio .NET 2003

Accomplishing drag-and-drop operations within Windows applications is done via the handling of a series of events, most notably the DragEnter, DragLeave, and DragDrop events. By working with the information available in the event arguments of these events, you can easily facilitate dragging and dropping.

Dragging Data

All drag-and-drop operations begin with dragging. The functionality to enable data to be collected when dragging begins is implemented in the DoDragDrop method.

In the following example, the MouseDown event is used to start the drag operation because it is the most intuitive (most drag-and-drop actions begin with the mouse button being depressed). However, remember that any event could be used to initiate a drag-and-drop procedure.

Note   Certain controls have custom drag-specific events. The ListView and TreeView controls, for example, have an ItemDrag event.

To start a drag operation

  • In the MouseDown event for the control where the drag will begin, use the DoDragDrop method to set the data to be dragged and the allowed effect dragging will have. For more information, see DataEventArgs.Data Property and DataEventArgs.AllowedEffect Property.

    The following example shows how to initiate a drag operation. The control where the drag begins is a Button control, the data being dragged is the string representing the Text property of the Button control, and the allowed effects are either copying or moving.

    ' Visual Basic
    Private Sub Button1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseDown
       Button1.DoDragDrop(Button1.Text, DragDropEffects.Copy Or DragDropEffects.Move)
    End Sub
    
    // C#
    private void button1_MouseDown(object sender, 
    System.Windows.Forms.MouseEventArgs e)
    {
       button1.DoDragDrop(button1.Text, DragDropEffects.Copy | 
          DragDropEffects.Move);
    }
    
    Note   Be aware that any data can be used as a parameter in the DoDragDrop method; in the example above, the Text property of the Button control was used (rather than hard-coding a value or retrieving data from a dataset) because the property was related to the location being dragged from (the Button control). Keep this in mind as you incorporate drag-and-drop operations into your Windows applications.

While a drag operation is in effect, you can handle the QueryContinueDrag event, which "asks permission" of the system to continue the drag operation. When handling this method, it is also the appropriate point for you to call methods that will have an effect on the drag operation, such as expanding a TreeNode in a TreeView control when the cursor hovers over it.

Dropping Data

Once you have begun dragging data from a location on a Windows Form or control, you will naturally want to drop it somewhere. The cursor will change when it crosses an area of a form or control that is correctly configured for dropping data. Any area within a Windows Form or control can be made to accept dropped data by setting the AllowDrop property and handling the DragEnter and DragDrop events.

To perform a drop

  1. In the Properties window, set the AllowDrop property to true.
  2. Right-click the form in Solution Explorer and choose View Code.
  3. In the DragEnter event for the control where the drop will occur, use an If statement to do type-checking to ensure the data being dragged is of an acceptable type (in this case, Text). The code then sets the effect that will happen when the drop occurs to a value in the DragDropEffects enumeration. For more information, see DragEventArgs.Effect Property.
    ' Visual Basic
    Private Sub TextBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragEnter
       If (e.Data.GetDataPresent(DataFormats.Text)) Then
         e.Effect = DragDropEffects.Copy
       Else
         e.Effect = DragDropEffects.None
       End If
    End Sub
    
    // C#
    private void textBox1_DragEnter(object sender, 
    System.Windows.Forms.DragEventArgs e)
    {
       if (e.Data.GetDataPresent(DataFormats.Text)) 
          e.Effect = DragDropEffects.Copy;
       else
          e.Effect = DragDropEffects.None;
    }
    
    Note   Be aware that you can define your own DataFormats, as well. This is as simple as specifying your own object as the Object parameter of the SetData method. Be sure, when doing this, that the object specified is serializable. For more information, see ISerializable Interface.
  4. In the DragDrop event for the control where the drop will occur, use the GetData method to retrieve the data being dragged. For more information, see DataObject.Data Property.

    In the example below, a TextBox control is the control being dragged to (where the drop will occur). The code sets the Text property of the TextBox control equal to the data being dragged.

    ' Visual Basic
    Private Sub TextBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragDrop
       TextBox1.Text = e.Data.GetData(DataFormats.Text).ToString
    End Sub
    
    // C#
    private void textBox1_DragDrop(object sender, 
    System.Windows.Forms.DragEventArgs e)
    {
       textBox1.Text = e.Data.GetData(DataFormats.Text).ToString();
    }
    
    Note   Additionally, you can work with the KeyState property, so that, depending on keys depressed during the drag-and-drop operation, certain effects occur (for example, it is standard to copy the dragged data when the CTRL key is pressed).

See Also

Placing Data on the Clipboard | Retrieving Information from the Clipboard | Drag-and-Drop Operations and Clipboard Support

Show:
© 2015 Microsoft