C#拖放DataObject [英] c# drag drop DataObject
问题描述
我正在使用C#2.0,我创建了一个资源管理器树,可以将信息从中拖到Windows窗体中。现在,当我从即时通讯使用的树状视图中拖动时,它会执行 DoDragDrop(selectedpath,DragDropEffects.Copy);
Im using C# 2.0 and i have created an explorer tree that i can drag information from into a windows form. right now, when i drag from the treeview that im using, it performs DoDragDrop(selectedpath, DragDropEffects.Copy);
当我在主窗体中捕获该事件时,它会以字符串形式列出。我能够完成这项工作,但我希望它执行与从Windows资源管理器窗口中拖动文件的操作相同的方式,如下所示:
When i catch the event in my mainform, it is listed as a string. I am able to make this work but i want it to perform the same way that it does if i were to drag a file from my windows explorer window which is as follows
Array name = (Array)e.Data.GetData(DataFormats.FileDrop);
// Ensure that the list item index is contained in the data.
if (e.Data.GetDataPresent(typeof(System.String)))
{
Object item = (object)e.Data.GetData(typeof(System.String));
// Perform drag-and-drop, depending upon the effect.
if (e.Effect == DragDropEffects.Copy ||
e.Effect == DragDropEffects.Move)
{
//// Insert the item.
//if (indexOfItemUnderMouseToDrop != ListBox.NoMatches)
// ListDragTarget.Items.Insert(indexOfItemUnderMouseToDrop, item);
//else
// ListDragTarget.Items.Add(item);
}
}
我尝试做DoDragDrop(新的DataObject(selectedfile),DragDropEffects.Copy),但是不起作用。
i have tried to do DoDragDrop(new DataObject(selectedfile),DragDropEffects.Copy) but that doesnt work.
推荐答案
DoDragDrop
和 DragDropEffects.Copy
不会对驱动器采取任何措施,除非您告诉他们这样做。 DragDropEffects.Copy
的作用实际上是在程序中复制对象 ,而不是文件本身。
DoDragDrop
and DragDropEffects.Copy
will not take action on your drive, not unless you tell them to. What DragDropEffects.Copy
does is actually copy the object in the program, not the file itself.
请参阅 DragDropEffects
。
您需要管理 OnDragDrop
事件,并使用 File.Copy
You'll need to manage the OnDragDrop
event and use a copy function like File.Copy
这篇关于C#拖放DataObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!