我怎么拖&放;滴在同一个列表视图的项目? [英] how do i drag & drop items in the same listview?

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

问题描述

认为这是显示我的文件/文件夹列表视图......我已经codeD一切都像(复制/移动/重命名/显示性能...等)..我只是需要更多的最后一件事这..我如何拖放在同一个列表视图..喜欢的图片..我的移动/复制方法,工作正常..我需要得到的物品在某些文件夹中的IM下探。在其他的方式
我需要得到这两个参数来调用复制的方法

 无效复印件(ListViewItem的[] droppedItems,串目标路径)
{
 //我得到了我的工作code在这里
}


解决方案

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

 布尔privateDrag;    私人无效listView1_ItemDrag(对象发件人,ItemDragEventArgs E){
        privateDrag = TRUE;
        的DoDragDrop(e.Item,DragDropEffects.Copy);
        privateDrag = FALSE;
    }

接下来,您需要DragEnter事件,将立即触发:

 私人无效listView1_DragEnter(对象发件人,DragEventArgs E){
        如果(privateDrag)e.Effect = e.AllowedEffect;
    }

接下来你要有所选择什么项目,用户可以拖放。这需要该项目正在徘徊DragOver事件和检查。你需要区分重新present从常规文件项的文件夹项目。可以这样做的一种方式是通过使用ListViewItem.Tag属性。例如,你可以将其设置到该文件夹​​的路径。制作这种code的工作:

 私人无效listView1_DragOver(对象发件人,DragEventArgs E){
        VAR POS = listView1.PointToClient(新点(e.X,e.Y));
        VAR命中= listView1.HitTest(POS)
        如果(hit.Item = NULL&放大器;!&安培;!hit.Item.Tag = NULL){
            VAR dragItem =(ListViewItem的)e.Data.GetData(typeof运算(ListViewItem的));
            副本(dragItem,(串)hit.Item.Tag);
        }
    }

如果你想支持拖动多个项目,然后进行拖动对象的ListView.SelectedIndices属性。

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
}

解决方案

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

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

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

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

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

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