C# - 将项目从列表视图拖放到垃圾桶? [英] C# - Drag item out of listview into a trash can?

查看:129
本文介绍了C# - 将项目从列表视图拖放到垃圾桶?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将项目从Winforms-listview控件拖出到另一个控件(垃圾桶图片)?

How do I drag a item out of a Winforms-listview control onto another control (picture of trash can)?

更新1:

我认为基本流程是:


  • listview有一个DoDragDrop

  • 然后在picturebox上有一个DragEnter事件捕捉到这个拖动?

更新2:

基本流程(基于答案):

The basic flow (based on answers):


  • 将'ItemDrag'事件添加到listview中。

  • 在'ItemDrag'中添加'DoDragDrop'

  • 添加'DragEnter '事件到图片框。

  • 在'DragEnter'中添加一个'GetDataPresent'检查来检查数据类型

  • 添加一个'DragDrop'事件到picturebox

  • 在'DragEnter'中添加一个'GetDataPresent'检查来检查数据类型

  • add 'ItemDrag' event to the listview.
  • add a 'DoDragDrop' inside the 'ItemDrag'
  • add 'DragEnter' event to the picturebox.
  • add a 'GetDataPresent' check inside the 'DragEnter' to check on the data type
  • add a 'DragDrop' event to the picturebox
  • add a 'GetDataPresent' check inside the 'DragEnter' to check on the data type

推荐答案

实现a列表视图的ItemDrag事件的事件处理函数:

Implement an event handler for the list view's ItemDrag event:

    private void listView1_ItemDrag(object sender, ItemDragEventArgs e) {
        DoDragDrop(e.Item, DragDropEffects.Move);
    }

并为垃圾桶写入事件处理程序:

And write the event handlers for the trash can:

    private void trashCan_DragEnter(object sender, DragEventArgs e) {
        if (e.Data.GetDataPresent(typeof(ListViewItem))) {
            e.Effect = DragDropEffects.Move;
        }
        // others...
    }

    private void trashCan_DragDrop(object sender, DragEventArgs e) {
        if (e.Data.GetDataPresent(typeof(ListViewItem))) {
            var item = e.Data.GetData(typeof(ListViewItem)) as ListViewItem;
            item.ListView.Items.Remove(item);
        }
        // others...
    }

你必须强制AllowDrop属性为PictureBox,它不可用在属性窗口中:

You'll have to force the AllowDrop property for the PictureBox, it isn't available in the Properties window:

    public Form1() {
        InitializeComponent();
        trashCan.AllowDrop = true;
    }

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

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