Drag and drop in listview C# [duplicate]

故事扮演 提交于 2019-12-24 03:48:15

问题


I currently trying to have my program able to drag a file and and drop it into the listview. I have see many sample and most of them have the following code:

private void listView1_draganddrop(object sender,  DragEventArgs e)

However after I implmeneted those code I have the few error i encounter..first is there no overload for listview1_SelectedIndexChanged match delegate System event handler

Another prob is the after the code is implmeneted I cannot drag any file into the listview.

I have enable the allow drop on my listview. So I was wondering what did I lack of to enable drag and drop function in c# and how to I code the drag and drop.


回答1:


Same answer I provided here: Drag and drop listview C#

You need to implement the DragEnter event and set the Effect property of the DragEventArgs. The DragEnter event is what allows things to be dropped into a control. After that the DragDrop event will fire when the mouse button is released.

Here is a version that will allow objects to be dropped into the a ListView:

    private void Form1_Load(object sender, EventArgs e)
    {
        listView1.AllowDrop = true;
        listView1.DragDrop += new DragEventHandler(listView1_DragDrop);
        listView1.DragEnter += new DragEventHandler(listView1_DragEnter);
    }

    void listView1_DragEnter(object sender, DragEventArgs e)
    {
        e.Effect = DragDropEffects.Copy;
    }

    void listView1_DragDrop(object sender, DragEventArgs e)
    {
        listView1.Items.Add(e.Data.ToString());
    }

No doubt your sample code was taken from : http://msdn.microsoft.com/en-us/library/system.windows.forms.control.allowdrop(v=vs.71).aspx



来源:https://stackoverflow.com/questions/7142920/drag-and-drop-in-listview-c-sharp

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