【问题标题】:how to Drag&Drop in the same ListView?如何在同一个ListView中拖放?
【发布时间】:2012-04-19 10:40:17
【问题描述】:

我有一个小型文件浏览器应用程序 [WINFORMS],我使用 ListView 控件来浏览项目。
因此 listView 显示当前地址 [From local Computer] 中的文件和文件夹。
我需要启用拖放功能以让文件/文件夹轻松移动/复制到同一地址中的另一个文件夹。 每个项目都有一些特点:

  • (item.Text/item.Name) 具有文件/文件夹 Name
  • item.ToolTipText 具有文件/文件夹路径
  • item.SubItems[ 1 ].Text 对于文件,它将表示文件大小,例如 "13.45 MB",对于文件夹,它将是 string.Empty [但是,有几种方法可以知道它是文件还是文件夹]。

    我看过很多关于如何在 listview 中使用拖放的教程,但这就像从 Windows 文件资源管理器到 ListView,或从 ListView 到另一个,但就我而言,我需要知道如何 拖放在同一个 ListView 中

    我已启用 ListView 的 AllowDrop 属性。并且还激活了以下功能:

        private void listView1_DragDrop(object sender, DragEventArgs e)
        {
        }
    
        private void listView1_DragEnter(object sender, DragEventArgs e)
        {
        }
    
        private void listView1_ItemDrag(object sender, ItemDragEventArgs e)
        {
        }
    

    更新:

    我试过用这个:

    private void listView1_DragDrop(object sender, DragEventArgs e)
        {
            ListViewItem item = (ListViewItem)e.Data.GetData(typeof(ListViewItem)); //this should be the target item (FOLDER)
            StringBuilder s = new StringBuilder();
            foreach (ListViewItem i in listView1.SelectedItems)
            {
                s.AppendLine(i.Text);
            }
            MessageBox.Show("DRAGGED ITEM : " + s.ToString() + "TARGET ITEM : " + item.Text);
        }
        private void listView1_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Copy;
        }
        private void listView1_MouseDown(object sender, MouseEventArgs e)
        {
            var item = listView1.GetItemAt(e.X, e.Y);
            if (item != null)
            {
                listView1.DoDragDrop(item, DragDropEffects.Copy);
            }
        }
    

    listView1_MouseDown 我得到了鼠标指向的项目,但它在我放下拖动的项目之前给了我项目,所以如果我将一个名为“SWImg”的文件夹拖到文件夹“ODDFiles”,消息框显示"SWImg - SWImg"

    然后我将listView1_MouseDown 替换为listView1_ItemDrag

    private void listView1_ItemDrag(object sender, ItemDragEventArgs e)
        {
            if (e.Item != null)
            {
                listView1.DoDragDrop(e.Item, DragDropEffects.Copy);
            }
        }
    

    同样的结果:S.

  • 【问题讨论】:

    • @Likurg 这对我不起作用。
    • 我会尽力为你做例子,但我明天再做,好吗?
    • @Likurg 我已经更新了我的问题,我从你的链接中尝试了一些东西。

    标签: c# winforms listview drag-and-drop


    【解决方案1】:

    我想通了。

    private void listView1_DragDrop(object sender, DragEventArgs e)
        {
            if (listView1.SelectedItems.Count == 0) { return; }
            Point p = listView1.PointToClient(MousePosition);
            ListViewItem item = listView1.GetItemAt(p.X, p.Y);
            if (item == null) { return; }
            List<ListViewItem> collection = new List<ListViewItem>();
            foreach (ListViewItem i in listView1.SelectedItems)
            {
                collection.Add((ListViewItem)i.Clone());
            }
            if ((e.Effect & DragDropEffects.Move) == DragDropEffects.Move)
            {
                Thread thMove = new Thread(unused => PasteFromMove(item.ToolTipText, collection));
                thMove.Start();
            }
            else
            {
                Thread thCopy = new Thread(unused => PasteFromCopy(item.ToolTipText, collection));
                thCopy.Start();
            }
    
        }
    
        private void listView1_DragOver(object sender, DragEventArgs e)
        {
            if ((e.KeyState & 8) == 8)
                e.Effect = DragDropEffects.Copy;
            else
                e.Effect = DragDropEffects.Move;
        }
    
        private void listView1_ItemDrag(object sender, ItemDragEventArgs e)
        {
            listView1.DoDragDrop(e.Item,DragDropEffects.All);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-02
      • 1970-01-01
      • 2012-07-18
      • 2016-11-04
      • 2012-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多