Joh*_*n M 6 c# drag-and-drop winforms
如何将项目从Winforms-listview控件拖到另一个控件(垃圾桶的图片)?
UPDATE1:
我认为基本流程是:
UPDATE2:
基本流程(基于答案):
Han*_*ant 14
为列表视图的ItemDrag事件实现事件处理程序:
private void listView1_ItemDrag(object sender, ItemDragEventArgs e) {
DoDragDrop(e.Item, DragDropEffects.Move);
}
Run Code Online (Sandbox Code Playgroud)
并为垃圾桶写下事件处理程序:
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...
}
Run Code Online (Sandbox Code Playgroud)
您必须强制PictureBox的AllowDrop属性,它在"属性"窗口中不可用:
public Form1() {
InitializeComponent();
trashCan.AllowDrop = true;
}
Run Code Online (Sandbox Code Playgroud)