5
votes

I've been having a hard time lately with implementing the drag and drop functionality outside the windows forms. I have no problem doing drag and drop within and between windows forms and from the desktop to the windows form. I have created an application where you can drag and drop any item on it. My problem is, I do not know how to implement the reverse of my application, to drag and drop from my app to the desktop or any destination outside my form. Any advise and ideas I will gratefully accept. Thank you.

we are talking about files and folders here ok :)

1
If you are dragging files that already exist, then an HDROP is all you need. If you want Explorer to create a new file based on what the user dragged, then you can use the samples on Creating something from nothing: Developer-friendly virtual file implementation for .NET. - Raymond Chen
You find some suggestions and samples here social.msdn.microsoft.com/Forums/en-US/winforms/thread/… - user629926

1 Answers

0
votes

I don't know which control you are using; most of the .net controls have a method DoDragDrop. Please use this method if it suits you.

private void PopulateListView()
{ 
    string directoryPath=Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
    String[] files=System.IO.Directory.GetFiles(directoryPath);
    if(files!=null)
    {
        foreach(string file in files)
        {
            listView1.Items.Add(new ListViewItem(file));
        }
    }
}


private void listView1_MouseDown(object sender, MouseEventArgs e)
{
    System.Collections.Specialized.StringCollection filePath = new
    System.Collections.Specialized.StringCollection();
    if (listView1.SelectedItems.Count > 0)
    { 
        filePath.Add(listView1.SelectedItems[0].Text);
        DataObject dataObject = new DataObject();
        dataObject.SetFileDropList(filePath);
        listView1.DoDragDrop(dataObject, DragDropEffects.Copy);
    }
}