拖放列表视图 C#

Drag and drop listview C#(拖放列表视图 C#)
本文介绍了拖放列表视图 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#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Force JsonConvert.SerializeXmlNode to serialize node value as an Integer or a Boolean(强制 JsonConvert.SerializeXmlNode 将节点值序列化为整数或布尔值)
Using JSON to Serialize/Deserialize TimeSpan(使用 JSON 序列化/反序列化 TimeSpan)
Could not determine JSON object type for type quot;Classquot;(无法确定类型“Class的 JSON 对象类型.)
How to deserialize a JSONP response (preferably with JsonTextReader and not a string)?(如何反序列化 JSONP 响应(最好使用 JsonTextReader 而不是字符串)?)
how to de-serialize JSON data in which Timestamp it-self contains fields?(如何反序列化时间戳本身包含字段的JSON数据?)
JSON.Net custom contract serialization and Collections(JSON.Net 自定义合约序列化和集合)