c# drag drop DataObject

旧时模样 提交于 2019-12-13 19:26:48

问题


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

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

            }
        }

i have tried to do DoDragDrop(new DataObject(selectedfile),DragDropEffects.Copy) but that doesnt work.


回答1:


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.

See the documentation on DragDropEffects.

You'll need to manage the OnDragDrop event and use a copy function like File.Copy



来源:https://stackoverflow.com/questions/6073953/c-sharp-drag-drop-dataobject

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!