当前位置: 首页 > news >正文

winform中的listbox实现拖拽功能

文章目录

  • 前言
  • 一、实现


前言

winform中的listBox实现拖拽!


一、实现

winform中的listbox实现拖拽只需要实现四个事件
1、准备两个listbox控件
在这里插入图片描述
其中listtarget,AllowDrop属性设置为True。
2、实现四个事件
2.1MouseDown

//在 MouseDown 事件期间,如果从鼠标位置起鼠标移动的距离大于 SystemInformation.DragSize,则启动拖动动作。
private void ListSource_MouseDown(object sender, MouseEventArgs e)
{//ListBox中Item项的索引indexOfItemUnderMouseToDrag = listSource.IndexFromPoint(e.X, e.Y);//鼠标悬停在列表框的​​有效项​​上(即 indexOfItemUnderMouseToDrag不是 -1)if (indexOfItemUnderMouseToDrag != ListBox.NoMatches){//记录鼠标按下位置,DragSize获取以鼠标按钮的按下点为中心的矩形的宽度和高度,在该矩形内不会开始拖动操作。Size dragSize =  SystemInformation.DragSize;//创建一个矩形区域(正方形)。以鼠标按下电为中心,以DragSize为高和宽的矩形。dragBoxFromMouseDown = new Rectangle(new Point(e.X - (dragSize.Width / 2), e.Y - (dragSize.Height / 2)), dragSize);}else{//如果鼠标没有选中ListBox项,则置矩形区域为空dragBoxFromMouseDown = Rectangle.Empty;}
}

2.2MouseMove

private void ListSource_MouseMove(object sender, MouseEventArgs e)
{/** 鼠标按钮状态是用​​位标志(bit flags)​​存储的:每个按钮对应一个二进制位可以同时检测多个按钮的状态&(按位与)操作可以提取特定按钮的状态* */if ((e.Button & MouseButtons.Left) == MouseButtons.Left){if (dragBoxFromMouseDown != Rectangle.Empty &&!dragBoxFromMouseDown.Contains(e.X,e.Y)){//传递ListBox选中项并触发DoDragDrop事件(这里可以是ListDragSoure触发,也可以是ListDragTarget)//DoDragDrop 方法确定当前光标位置下的控件。然后它将检查该控件是否是有效的放置目标。DragDropEffects dropEffect = listSource.DoDragDrop(listSource.Items[indexOfItemUnderMouseToDrag], DragDropEffects.All | DragDropEffects.Link);if(dropEffect != DragDropEffects.None){listSource.Items.RemoveAt(indexOfItemUnderMouseToDrag);if (indexOfItemUnderMouseToDrag > 0)listSource.SelectedIndex = indexOfItemUnderMouseToDrag - 1;else if (listSource.Items.Count > 0)listSource.SelectedIndex = 0;}}}
}

2.3DragDrop

private void Listtarget_DragDrop(object sender, DragEventArgs e)
{if (e.Data.GetDataPresent(typeof(String))){Object item = e.Data.GetData(typeof(String));if(e.Effect == DragDropEffects.Copy || e.Effect == DragDropEffects.Move){listtarget.Items.Add(item);}}
}

2.4DragEnter

/*
当用户拖动数据进入目标控件(例如一个ListBox或其他控件)时,此方法会被调用。
通过设置e.Effect = DragDropEffects.Move;,它告诉系统当前控件接受拖放操作,并且操作的类型是“移动”。
这个设置会改变鼠标光标的显示(通常会显示一个移动图标),给用户视觉反馈,表明松开鼠标后数据将被移动到这里。*/
private void Listtarget_DragEnter(object sender, DragEventArgs e)
{e.Effect = DragDropEffects.Move;
}

2.5绑定事件

private int indexOfItemUnderMouseToDrag;
private Rectangle dragBoxFromMouseDown;
public Form1()
{InitializeComponent();listSource.Items.AddRange(new string[] { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten" });listSource.MouseDown += ListSource_MouseDown;listSource.MouseMove += ListSource_MouseMove;listtarget.DragDrop += Listtarget_DragDrop;listtarget.DragEnter += Listtarget_DragEnter;
}

效果图
在这里插入图片描述
注意:如果你的窗体内未使用Combox控件,则看这篇文章就行了。
如果使用到了Combox控件,建议看一下这篇文章“解决winform中的listbox实现拖拽时,遇到combox控件会闪烁的问题”。

http://www.dtcms.com/a/318875.html

相关文章:

  • 基于ubuntu搭建gitlab
  • KDE Connect
  • 一篇文章入门TCP与UDP(保姆级别)
  • 02电气设计-安全继电器电路设计(让电路等级达到P4的安全等级)
  • C语言strncmp函数详解:安全比较字符串的实用工具
  • 合约收款方式,转账与问题安全
  • 怎么进行专项分析项目?
  • 上证50期权持仓明细在哪里查询?
  • C语言(08)——整数浮点数在内存中的存储
  • LINUX-批量文件管理及vim文件编辑器
  • 浅析 Berachain v2 ,对原有 PoL 机制进行了哪些升级?
  • AutoMQ-Kafka的替代方案实战
  • JAVA第六学:数组的使用
  • 【C++】哈希表原理与实现详解
  • 基于langchain的两个实际应用:[MCP多服务器聊天系统]和[解析PDF文档的RAG问答]
  • 智能制造的中枢神经工控机在自动化产线中的关键角色
  • 行业应用案例:MCP在不同垂直领域的落地实践
  • 二叉树算法之【中序遍历】
  • OpenAI重磅发布:GPT最新开源大模型gpt-oss系列全面解析
  • SpringBoot请求重定向目标地址不正确问题分析排查
  • 六类注定烂尾的甲方软件外包必看!这类甲方不要理-优雅草卓伊凡
  • 上门家教 app 用户端系统模块设计
  • 区块链简介
  • C++位图(Bitmap)与布隆过滤器(Bloom Filter)详解及海量数据处理应用
  • java excel转图片常用的几种方法
  • 分布式接口限流与防重复提交实现方案
  • 快速搭建vue3+flask实现一个异物检测项目
  • RP2040下的I2S Slave Out,PIO状态机(四)
  • MT信号四通道相关性预测的Informer模型优化研究
  • 此芯p1开发板使用OpenHarmony时llama.cpp不同优化速度对比(GPU vs CPU)