0

I have a small file explorer application [WINFORMS], and i use ListView Control to explore items.
So the listView display the files and the folders from the current address [From local Computer].
i need to enable drag&drop functionality to let a File/Folder easy to move/copy to another Folder in the same Address. each item has some preperties:

  • (item.Text/item.Name) has the file/folder Name.
  • item.ToolTipText has the file/folder Path.
  • item.SubItems[ 1 ].Text for a file it will respresent the file size like "13.45 MB" and for a folder, it's going to be string.Empty [However, there's several way to know whether its a file or a folder].

    I've seen many tutorials about how to use drag&drop in listview, but it was like from Windows File Explorer to ListView, or from a ListView to another one, but in my case i need to know how to drag&drop in the same ListView.

    I've Enabled AllowDrop property of the ListView. and also Activated the following functions:

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

    enter image description here

    UPDATE:

    i tried to use this :

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

    at listView1_MouseDown i get the item which mouse points onto, but it gives me the item before i drop the dragged item, so if i am dragging a folder named "SWImg" to the folder "ODDFiles" , the messageBox shows "SWImg - SWImg"

    then i replaced listView1_MouseDown with listView1_ItemDrag :

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

    same result :S.

  • Murhaf Sousli
    • 12,622
    • 20
    • 119
    • 185

    1 Answers1

    0

    I've figured it out.

    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);
        }
    
    Murhaf Sousli
    • 12,622
    • 20
    • 119
    • 185