DanO
DanO

Reputation: 417

Windows form with list views, dragging and dropping, need source list view

Using VS 2008 I have a windows form with 2 ListViews (we will call them ListView1 and ListView2). ListView1 is populated with FileNames from a directory. When an item is dragged from ListView1 to ListView2 I have some code that is executed. When I dragDrop from ListView2 to ListView1 some code executes. What I want to do is NOT execute the code if you dragdrop from ListView2 onto itself

Here is the dragDrop Method which is invoked after a drop:

private void view_DragDrop(object dropTarget, DragEventArgs e)

I have tried a few items such as below:

ListView data = (ListView)e.Data.GetData("System.Windows.Forms.ListView")

This returns null what I wanted to do with the above is see if data = dropTarget, do not execute.

Upvotes: 1

Views: 777

Answers (2)

Wolf-Kun
Wolf-Kun

Reputation: 678

create you a variable, in the scope of your form

object dfrom;
//your methods ect
    private void view_ItemDrag(Object sender, System.Windows.Forms.ItemDragEventArgs e)
    //your code to drag items
    dfrom = sender
}
private void view_DragDrop(object dropTarget, System.Windows.Forms.DragEventArgs e)
{
    if (dfrom == sender){return;}//this will protect both list boxes (assuming you can drag from both).
    //Your code
}

Upvotes: 0

Alexandre
Alexandre

Reputation: 4602

You can probably verify the source of the event and make sure the target is not the same by using the OriginalSource attribute.

So in your DragDrop event on your ListView2, I'd do something like this:

private void view_DragDrop(object dropTarget, DragEventArgs e)
{
    if (e.OriginalSource == ListView2)
       return;

    //Rest of the code here
}

This will prevent any items that got Dragged from ListView2 to trigger your code execution in ListView2.

DragEventArgs Class on MSDN

Upvotes: 1

Related Questions