How do I drag a item out of a Winforms-listview control onto another control (picture of trash can)?
UPDATE1:
I think the basic flow is:
UPDATE2:
The basic flow (based on answers):
Implement an event handler for the list view's ItemDrag event:
private void listView1_ItemDrag(object sender, ItemDragEventArgs e) {
DoDragDrop(e.Item, DragDropEffects.Move);
}
And write the event handlers for the trash can:
private void trashCan_DragEnter(object sender, DragEventArgs e) {
if (e.Data.GetDataPresent(typeof(ListViewItem))) {
e.Effect = DragDropEffects.Move;
}
// others...
}
private void trashCan_DragDrop(object sender, DragEventArgs e) {
if (e.Data.GetDataPresent(typeof(ListViewItem))) {
var item = e.Data.GetData(typeof(ListViewItem)) as ListViewItem;
item.ListView.Items.Remove(item);
}
// others...
}
You'll have to force the AllowDrop property for the PictureBox, it isn't available in the Properties window:
public Form1() {
InitializeComponent();
trashCan.AllowDrop = true;
}
Look into DragEnter
, DragLeave
, and DragDrop
. Also see example, Implementing Drag and Drop in ListView Controls
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With