前进中的蜗牛

番茄大叔

水滴穿石,非一日之功;没有量变,何来质变。

WinForm 拖拽(Drag and Drop)

WinForm 拖拽(Drag and Drop)

需求

winForm开发,将一个ListView中的项ListViewItem拖入到另一个ListView控件中。自己对WinForm 开发较少,折腾了一天才整出来。总结一下,为自己增加点WinForm开发经验。

Code

  1. 新建1ListBoxDrgDropListBoxForm,托人两个ListView分别命名为:'ListDragSource','ListDragTarget'
  2. 初始化ListDragSource中的数据
		private void InitListDragSource()
		{
			this.ListDragSource.Items.AddRange(new ListViewItem[] {
				new ListViewItem("one"), 
				new ListViewItem("two"), 
				new ListViewItem("three"), 
				new ListViewItem("four"), 
				new ListViewItem("five"), 
				new ListViewItem("six")
				});
		}
  1. 相关事件定义
	/// <summary>
		/// 拖拽ListView中的Item时触发事件
		/// </summary>
		/// <param name="sender">ListView</param>
		/// <param name="e">参数</param>
		private void ListDragSource_ItemDrag(object sender, ItemDragEventArgs e)
		{
			ListView lstView = sender as ListView;
			if (lstView != null)
			{
				lstView.DoDragDrop(lstView.SelectedItems, DragDropEffects.Move);
			}
		}

		/// <summary>
		/// 当ListView中的项(Item)托离ListView时触发
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void ListDragTarget_DragLeave(object sender, EventArgs e) 
		{
			ListView lstView = sender as ListView;

			if (lstView != null)
			{
				foreach (ListViewItem item in lstView.SelectedItems)
				{
					lstView.Items.Remove(item);
				}
			}
		}

		/// <summary>
		/// 拖拽完成时出发
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void ListDragTarget_DragDrop(object sender, DragEventArgs e)
		{
			ListView lstView = sender as ListView;
			System.Windows.Forms.ListView.SelectedListViewItemCollection items = (System.Windows.Forms.ListView.SelectedListViewItemCollection)e.Data.GetData(typeof(System.Windows.Forms.ListView.SelectedListViewItemCollection));
			if (items != null && items.Count > 0)
			{
				foreach (ListViewItem item in items)
				{
					ListViewItem itemTemp = (ListViewItem)item.Clone();
					itemTemp.Name = item.Text;
					if (!lstView.Items.ContainsKey(itemTemp.Name))
					{
						lstView.Items.Add(itemTemp);
					}
				}
			}
		}

		/// <summary>
		/// 当拖入ListView时除非
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void ListDragTarget_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
		{

			e.Effect = DragDropEffects.Move;
		}
  1. ListView拖动属性设置和事件绑定
	private void ListBoxDrgDropListBox_Load(object sender, EventArgs e)
		{
			InitListDragSource();
			//设置控件支持拖动
			this.ListDragTarget.AllowDrop = true;
			this.ListDragSource.AllowDrop = true;

			this.ListDragSource.ItemDrag += new ItemDragEventHandler(ListDragSource_ItemDrag);
			this.ListDragTarget.ItemDrag += new ItemDragEventHandler(ListDragSource_ItemDrag);

			this.ListDragTarget.DragLeave += new EventHandler(ListDragTarget_DragLeave);
			this.ListDragTarget.DragEnter += new DragEventHandler(ListDragTarget_DragEnter);
			this.ListDragTarget.DragDrop += new DragEventHandler(ListDragTarget_DragDrop);
		}

注意

  1. 设置ListView控件支持拖动
  2. 拖动事件ItemDrag(指ListView控件中项事件)首先触发,必须定义实现此事件,否则后面事件不触发
  3. 防止重复数据拖入lstView.Items.ContainsKey(itemTemp.Name),其中Key即为ListViewItem实例的Name
posted @   LoveTomato  阅读(1161)  评论(1编辑  收藏  举报
编辑推荐:
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器
点击右上角即可分享
微信分享提示