拖放列表视图 C# [英] Drag and drop listview C#

查看:18
本文介绍了拖放列表视图 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

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

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