Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 492
user1056128 Avatar asked Nov 25 '22 06:11

user1056128


1 Answers

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);
    }
}
like image 158
MuraliAsok Avatar answered Dec 20 '22 22:12

MuraliAsok