开发者

Drag a file url from a winforms control to external application

i need to drag rows representing files from a datagridview, and be able to drop them to any windows application as if i was doing a drag from the windows explorer, and i'm a little bit confused regarding the procedure to follow.

For the moment i have the following event handler

private void gridFiles_MouseDown(object sender, MouseEventArgs e)
{
    gridFiles.DoDragDrop(gridFiles.SelectedRows.Count, DragDropEffects.Move);
}

The thing is that i also need to be able to drop the rows into a control inside my application, and that in this case i woul开发者_Go百科d like to be able to get application specific information about the rows.


I finally figured it out. The procedure is as follows :

  • Create a DataObject which will contain an array of file paths.
  • Set the DataObject type to FileDrop
  • Pass the DataObject to the DoDragDrop procedure

Sample code :

if (is_in_selection)
{
    sel_rows = from DataGridViewRow r in gridFiles.SelectedRows select r;
    var files = (from DataGridViewRow r in gridFiles.SelectedRows select all_files[r.Index]);
    string[] files_paths = files.Select((f) => f.FullPathName).ToArray();
    var data = new DataObject(DataFormats.FileDrop, files_paths);
    gridFiles.DoDragDrop(data, DragDropEffects.Copy);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜