Drag and drop text in C#

[Drag and drop]

This example shows how to let a program drag and drop text both between its controls and with drag and drop enabled controls in other applications.

First, to receive a drop, you must set a control’s AllowDrop property to true. If you forget to do that (a very common mistake), then drag and drop seems to have no effect and you can become very confused trying to figure out why it won’t work.

To start a drag, use a control’s DoDragDrop method. The control that starts the drag is called the drag source. The following code starts a drag when the user presses the right mouse button down over the label lblDragSource.

// Start a drag that copies text.
private void lblDragSource_MouseDown(object sender,
    MouseEventArgs e)
{
    // Start the drag if it's the right mouse button.
    if (e.Button == MouseButtons.Right)
    {
        lblDragSource.DoDragDrop("Here's some text!",
            DragDropEffects.Copy);
    }
}

The code passes the data “Here’s some text!” to the DoDragDrop method. That tells the system what data you are offering to drop. The second parameter indicates that the control is offering to copy the text, not move it or perform some other action.

When a drag moves over a control that has AllowDrop = true, the DragEnter event fires. This control is called the drop target.

// Indicate that we can accept a copy of text.
private void lblDropTarget_DragEnter(object sender,
    DragEventArgs e)
{
    // See if this is a copy and the data includes text.
    if (e.Data.GetDataPresent(DataFormats.Text) &&
        (e.AllowedEffect & DragDropEffects.Copy) != 0)
    {
        // Allow this.
        e.Effect = DragDropEffects.Copy;
    }
    else
    {
        // Don't allow any other drop.
        e.Effect = DragDropEffects.None;
    }
}

The DragEnter event handler should examine the available drag types and decide whether it can accept the data. In this example, the program determines whether the data includes text and whether a copy operation is allowed.

A drag may allow other operations at the same time. That’s why the code uses & to see if e.AllowedEffect has the bit set corresponding to the Copy operation.

The code tells the drag and drop system what kind of cursor to display by setting e.Effect. In this example, if text is available for copying, the code sets the effect to Copy to indicate that the control can accept a copy. If text is not available or a copy operation is not offered by the drag source, the control allows no effect.

Finally when the user drops text over a drop target that allows the current operation, the DragDrop event fires.

// Accept the drop.
private void lblDropTarget_DragDrop(object sender, DragEventArgs e)
{
    lblDropTarget.Text = (string)e.Data.GetData(DataFormats.Text);
}

In this example, the code gets the data object e.Data, uses its GetData method to get the text data that’s available, and displays it in the drop target label lblDropTarget.

Note that the drag and drop system runs across all applications so any program that supports drag and drop can participate. For example, try running the example program and then dragging from the example to Microsoft Word or from Word to the example program’s drop target.


Download Example   Follow me on Twitter   RSS feed   Donate




This entry was posted in Drag and Drop, interoperability and tagged , , , , , , , , , , . Bookmark the permalink.

2 Responses to Drag and drop text in C#

  1. Corey says:

    This is cool! Thanks man.

  2. Pingback: Drag and drop images in C# - C# HelperC# Helper

Leave a Reply

Your email address will not be published. Required fields are marked *