Murhaf Sousli
Murhaf Sousli

Reputation: 13296

ListView Drag&Drop, how to check if im dropping the item in the same listview or not?

i have a two listview [listView1, listLocal], they are two file explorers for local PC and remote PC
if i am dropping items into the same listview that i dragged them from, it should copy/move. else if i am dropping item into the other listview then it should send/receive.

so let listLocal is the file explorer for local PC, and listView1 is for remote PC.
so what i need is a condition that checks if i'm dropping the dragged item into the other listview or not.

private void listLocal_ItemDrag(object sender, ItemDragEventArgs e)
    {
        if (_currAddress == null) return;   //if the current address is My Computer
        _DraggedItems.Clear();
        foreach (ListViewItem item in listLocal.SelectedItems)
        {
            _DraggedItems.Add((ListViewItem)item.Clone());
        }
        // if ( some condition )   call the same listview **listLocal.DoDragDrop**
        listLocal.DoDragDrop(e.Item, DragDropEffects.All);
        // else                    call the other listview
        listView1.DoDragDrop(e.Item, DragDropEffects.All);
    }

also when a listview DragDrop fired i need i condition that checks if the dragged item came from the same listview or not.

private void listView1_DragDrop(object sender, DragEventArgs e)
    {
        Point p = listView1.PointToClient(MousePosition);
        ListViewItem targetItem = listView1.GetItemAt(p.X, p.Y);
        // if ( some condition )     
        //here i need to check if the dragged item came from the same listview or not
        {
            if (targetItem == null)
            {
                PreSend(currAddress);          //send to the current address
            }
            else
            {
                PreSend(targetItem.ToolTipText);        //send to the target folder
            }
            return;
        }
        //otherwise

        if (targetItem == null) { return; }          //if dropped in the same folder return

        if ((e.Effect & DragDropEffects.Move) == DragDropEffects.Move)
        {
            Thread thMove = new Thread(unused => PasteFromMove(targetItem.ToolTipText, DraggedItems));
            thMove.Start();
        }
        else
        {
            Thread thCopy = new Thread(unused => PasteFromCopy(targetItem.ToolTipText, DraggedItems));
            thCopy.Start();
        }
    }

Upvotes: 1

Views: 2169

Answers (1)

Hans Passant
Hans Passant

Reputation: 942177

You need to implement the DragEnter event to verify that the proper object is being dragged. You can use the ListViewItem.ListView property to verify that it came from another ListView. Like this:

    private void listView1_DragEnter(object sender, DragEventArgs e) {
        // It has to be a ListViewItem
        if (!e.Data.GetDataPresent(typeof(ListViewItem))) return;
        var item = (ListViewItem)e.Data.GetData(typeof(ListViewItem));
        // But not one from the same ListView
        if (item.ListView == listView1) return;
        // All's well, allow the drop
        e.Effect = DragDropEffects.Copy;
    }

No further checks are required in the DragDrop event handler.

Upvotes: 2

Related Questions