拖放listview C# [英] Drag and drop listview C#

查看:120
本文介绍了拖放listview C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在双击列表视图时启用拖动事件处理程序?

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)

但是,我想要它是

private void listView(object sender,DragEventArgs e)

我该怎么做..?

我尝试了很多方法,如:

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


推荐答案

您需要实现 DragEnter 事件,并设置DragEventArgs的Effect属性。 DragEnter 事件是将事情放入控件中。之后,当鼠标按钮被释放时, DragDrop 事件将触发。

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.

这是一个允许将对象放入ListView的版本:

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

毫无疑问,您的示例代码取自: http://msdn.microsoft.com/en -us / library / system.windows.forms.control.allowdrop(v = vs.71).aspx

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

这篇关于拖放listview C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆