user1056128
user1056128

Reputation: 73

Drag and drop from windows forms to desktop and windows explorer

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 :)

Upvotes: 6

Views: 6356

Answers (1)

MuraliAsok
MuraliAsok

Reputation: 21

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);
    }
}

Upvotes: 1

Related Questions