gpt4 book ai didi

C# - 将项目从 ListView 拖到垃圾桶中?

转载 作者:太空狗 更新时间:2023-10-29 20:52:15 25 4
gpt4 key购买 nike

如何将项目从 Winforms-listview 控件拖到另一个控件(垃圾桶图片)上?

更新 1:

我认为基本流程是:

  • 对于 ListView 上的 ItemDrag 事件有一个 DoDragDrop
  • 然后在捕获拖动的图片框上有一个 DragEnter 事件?

更新 2:

基本流程(基于答案):

  • 将“ItemDrag”事件添加到 ListView 。
  • 在“ItemDrag”中添加一个“DoDragDrop”
  • 向图片框添加“DragEnter”事件。
  • 在“DragEnter”中添加“GetDataPresent”检查以检查数据类型
  • 向图片框添加“拖放”事件
  • 在“DragEnter”中添加“GetDataPresent”检查以检查数据类型

最佳答案

为 ListView 的 ItemDrag 事件实现一个事件处理程序:

    private void listView1_ItemDrag(object sender, ItemDragEventArgs e) {
DoDragDrop(e.Item, DragDropEffects.Move);
}

并为垃圾桶编写事件处理程序:

    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...
}

您必须强制 PictureBox 的 AllowDrop 属性,它在“属性”窗口中不可用:

    public Form1() {
InitializeComponent();
trashCan.AllowDrop = true;
}

关于C# - 将项目从 ListView 拖到垃圾桶中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2876139/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com