C#中两个listBox双击与拖放共存的一种实现
来源:互联网 发布:好听的网络歌曲大全 编辑:程序博客网 时间:2024/09/21 09:03
今天做前台C#的同事,纠结在了,“拖拽事件使用的listBox1_MouseDown(s, e)会‘屏蔽掉’双击事件的使用的listBox1_DoubleClick”,这一问题上。查证多方资料,没好的解决方法。
于是笔者休息时实验了一下,使用e.Clicks这个属性可以解决。具体请参阅正文。
正文:
实现机理:((MouseEventArgs)e).Clicks通过值的{1, 2, ...}可以区分单击双击。于是可将双击事件实现写入e.Clicks > 1的语句,来达到预期效果。
细节不叨叨,直接上代码。
实验效果图如下:
于是笔者休息时实验了一下,使用e.Clicks这个属性可以解决。具体请参阅正文。
正文:
实现机理:((MouseEventArgs)e).Clicks通过值的{1, 2, ...}可以区分单击双击。于是可将双击事件实现写入e.Clicks > 1的语句,来达到预期效果。
细节不叨叨,直接上代码。
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- namespace C4PlusWForm
- {
- public partial class Form2 : Form
- {
- public Form2()
- {
- InitializeComponent();
- }
- private void listBox1_MouseDown(object sender, MouseEventArgs e)
- {
- // 双击后触发动作
- if (e.Clicks > 1)
- {
- listBox2.Items.Add(listBox1.SelectedItem);
- listBox1.Items.Remove(listBox1.SelectedItem);
- System.Console.WriteLine("listBox1_MouseDown...DoubleClick");
- }
- // 单击动作
- else {
- int index = listBox1.IndexFromPoint(e.X, e.Y);
- string str = listBox1.Items[index].ToString();
- DragDropEffects ddeLb1 = DoDragDrop(str, DragDropEffects.All);
- if (ddeLb1 == DragDropEffects.All)
- {
- listBox1.Items.RemoveAt(listBox1.IndexFromPoint(e.X, e.Y));
- }
- }
- System.Console.WriteLine("listBox1_MouseDown" + e.Clicks);
- }
- private void listBox2_DragDrop(object sender, DragEventArgs e)
- {
- if (e.Data.GetDataPresent(DataFormats.StringFormat))
- {
- string str = (string)e.Data.GetData(
- DataFormats.StringFormat);
- listBox2.Items.Add(str);
- }
- }
- private void listBox2_DragOver(object sender, DragEventArgs e)
- {
- e.Effect = DragDropEffects.All;
- }
- private void listBox1_DoubleClick(object sender, EventArgs e)
- {
- System.Console.WriteLine("listBox1_DoubleClick");
- }
- }
- }
实验效果图如下:
- C#中两个listBox双击与拖放共存的一种实现【转】
- C#中两个listBox双击与拖放共存的一种实现
- 实现C# listbox双击
- C#中让ListBox支持文件路径的拖放
- listbox实现拖放操作
- 在C#中实现两个listbox的项移动(winform)
- ListBox中增加item的双击事件
- C#中实现拖放操作
- Winfrom 中两个listBox实现值的相互传递
- 实现文件拖放的一种简洁方法
- 实现文件拖放的一种简洁方法
- 实现文件拖放的一种简洁方法
- 实现文件拖放的一种简洁方法
- 实现文件拖放的一种简洁方法
- 实现文件拖放的一种简洁方法
- 在C#中实现listbox的项添加与移除(winform)
- c#中实现文件拖放打开的方法
- C#中实现文件拖放打开的方法
- Python学习笔记:字符串基本操作
- 1月浏览器排行:IE10突破1% Chrome连续3月下滑
- 远程处理超过连接数
- (一)Java解析XML:XML基本概念的理解
- Mark 下 Rational Rose2003
- C#中两个listBox双击与拖放共存的一种实现
- 转一个J2EE开发时的包命名规则,养成良好的开发习惯
- 初始jquery mobile
- 本地连接受限制或无连接 的解决方法
- 【Android】Android学习过程中的一些网站
- 基于avd7181c解决视频输入效果差的问题<二>---疑难调试手记
- 新年
- 学习--UML小结
- GOOGLE C++笔记(三)