Dragging a filename from a listbox to the desktop and having the file copied there

dipique

Member
Joined
Oct 22, 2014
Messages
7
Programming Experience
1-3
I have filenames in a listbox and would like to drag an item from the listbox onto the desktop and have the file copied there. Here's the code I currently use when drag:

[XCODE] private void lstResults_MouseMove(object sender, MouseEventArgs e)
{
//If the mouse is in a draggable state and there is a selected item
if (_canDrag && (lstResults.SelectedIndex != -1))
//make sure the string refers to a real, existing file
if (File.Exists(lstResults.SelectedItem.ToString()))
{
//Drag and drop!
_canDrag = false;
DataObject dtObj = new DataObject(DataFormats.FileDrop, lstResults.SelectedItem.ToString());
DoDragDrop(dtObj, DragDropEffects.Copy);
}
}
[/XCODE]

With this code, I can drag and drop and the mouse icon looks correct, but when I drop no file is copied. Help!
 
Wow. That was an embarrassingly easy fix.

Follow up question: I notice it now works on the desktop, but doesn't work to drag into an Outlook e-mail. Can you point me in the right direction of how to determine what Outlook expects?
 
Wow. That was an embarrassingly easy fix.

Follow up question: I notice it now works on the desktop, but doesn't work to drag into an Outlook e-mail. Can you point me in the right direction of how to determine what Outlook expects?
It expects no different I'd say, because the same code works just fine when I drag-drop to an outbound email in Outlook.
For expectations, FileDrop is also a system standard, that I would expect any application that allows file drops to support.
 
Back
Top Bottom