Drag and drop listview C#

做~自己de王妃 提交于 2019-12-19 11:49:16

问题


Hi how to I enable drag event handler when I double click on the listview?

This is what I get after double-clicking on the listview

private void listView1(object sender, EventArgs e)

However, I want it to be

private void listView(object sender,DragEventArgs e)

How to I do it..?

I have tried many way such as:

  private void Form_Load(object sender, EventArgs e)
  {
      // Enable drag and drop for this form
      // (this can also be applied to any controls)
      this.AllowDrop = true;

      // Add event handlers for the drag & drop functionality
      this.DragEnter += new DragEventHandler(Form_DragEnter);
      this.DragDrop += new DragEventHandler(Form_DragDrop);
 }

回答1:


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/7143122/drag-and-drop-listview-c-sharp

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