RV.
RV.

Reputation: 2842

Drag and Drop in C#?

How to Implement Drag and Drop Between my Program and Explorer ya windows application only

Upvotes: 4

Views: 3811

Answers (3)

Noldorin
Noldorin

Reputation: 147240

As long as you're using WinForms, it's actually very straightforward. See these two articles to get you started:

And just in case you're using WPF, this tutorial and this SO thread should help.

Upvotes: 5

Eduardo Crimi
Eduardo Crimi

Reputation: 1601

Add this on the Drag enter event (this will change the cursor type when you are dragging a file)

 private void Form1_DragEnter(object sender, DragEventArgs e)
    {
        // If file is dragged, show cursor "Drop allowed"
        if (e.Data.GetDataPresent(DataFormats.FileDrop))
            e.Effect = DragDropEffects.Copy;
        else
            e.Effect = DragDropEffects.None;
    }

Then on the DragDrop event you need to handle what do ou want to do. And also set the AllowDrop property to true

Upvotes: 1

splattne
splattne

Reputation: 104030

There is a good article on CodeProject about how to do this:

This sample project lists a folder full of files, and lets you drag and drop them into Explorer. You can also drag from Explorer into the sample, and you can use the Shift and Ctrl keys to modify the action, just like in Explorer.

Drag and drop, cut/copy and paste files with Windows Explorer

To start a drag operation into Explorer, we implement the ItemDrag event from the Listview, which gets called after you drag an item more than a few pixels. We simply call DoDragDrop passing the files to be dragged wrapped in a DataObject. You don't really need to understand DataObject - it implements the IDataObject interface used in the communication.

Upvotes: 1

Related Questions