我怎么拖&删除项目在同一个listview? [英] how do i drag & drop items in the same listview?

查看:250
本文介绍了我怎么拖&删除项目在同一个listview?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个listview显示我文件/文件夹...我已经编码的一切像(复制/移动/重命名/显示属性...)..我只需要更多的最后一件事..我如何拖动和在同一个listview ..像在图片..我有移动/复制方法工作正常..我需要得到的项目im放在一些文件夹..以其他方式
i需要得到这两个参数调用复制方法

consider that this is a listview that shows me files/folders ... i've already coded everything like (copy/move/rename/show properties...etc) .. i just need more last thing which .. how do i drag and drop in the same listview .. like in the picture .. i've the move/copy methods works fine .. i need to get the items im dropping in some folder .. in other way i need to get these two parameters to call copy method

void copy(ListViewItem [] droppedItems, string destination path)
{
 // i got my working code here
}


推荐答案

首先将列表视图的AllowDrop属性设置为true。实现ItemDrag事件以检测拖动的开始。我将使用一个私有变量来确保D + D只在控件内部起作用:

Start by setting the list view's AllowDrop property to true. Implementing the ItemDrag event to detect the start of a drag. I'll use a private variable to ensure that D+D only works inside of the control:

    bool privateDrag;

    private void listView1_ItemDrag(object sender, ItemDragEventArgs e) {
        privateDrag = true;
        DoDragDrop(e.Item, DragDropEffects.Copy);
        privateDrag = false;
    }

接下来你需要DragEnter事件, p>

Next you'll need the DragEnter event, it will fire immediately:

    private void listView1_DragEnter(object sender, DragEventArgs e) {
        if (privateDrag) e.Effect = e.AllowedEffect;
    }

接下来,您将需要选择性地讨论用户可以选择的项目。这需要DragOver事件并检查哪个项目被悬停。您需要将代表文件夹的项目与常规文件项目区分开来。一种方法是使用ListViewItem.Tag属性。例如,您可以将其设置为文件夹的路径。使此代码工作:

Next you'll want to be selective about what item the user can drop on. That requires the DragOver event and checking which item is being hovered. You'll need to distinguish items that represent a folder from regular 'file' items. One way you can do so is by using the ListViewItem.Tag property. You could for example set it to the path of the folder. Making this code work:

    private void listView1_DragOver(object sender, DragEventArgs e) {
        var pos = listView1.PointToClient(new Point(e.X, e.Y));
        var hit = listView1.HitTest(pos);
        if (hit.Item != null && hit.Item.Tag != null) {
            var dragItem = (ListViewItem)e.Data.GetData(typeof(ListViewItem));
            copy(dragItem, (string)hit.Item.Tag);
        }
    }

如果您要支持拖动多个项目,拖动对象的ListView.SelectedIndices属性。

If you want to support dragging multiple items then make your drag object the ListView.SelectedIndices property.

这篇关于我怎么拖&删除项目在同一个listview?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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