引言:
今天做前台C#的同事,纠结在了,“拖拽事件使用的listBox1_MouseDown(s, e)会‘屏蔽掉’双击事件的使用的listBox1_DoubleClick”,这一问题上。查证多方资料,没好的解决方法。
于是笔者休息时实验了一下,使用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");
}
}
}
实验效果图如下:
操作步骤:
0)选中222
1)拖动222
2)双击333
3)双击1111
- 大小: 21.2 KB
分享到:
相关推荐
使用Dev控件实现的Tree的DragDrop的小例子
C# WinForm两个listBox之间的Item互相拖动
有时,我们程序,需要加载一个文件列表,这个列表,通常用ListBox来存放,为了方便操作,让listBox支持拖放入文件的路径,是个非常好的功能,在.NET里面实现这个功能,是非常方便,只需要3个步骤:
通过继承UserControl控件,重新写了一个可编辑的ListBox,它可以实现直接在框内点击添加,修改等。
listbox与textbox组合,实现类似google、百度搜索的模糊查询功能。textbox中输入数据,连接数据库进行模糊查找数据库记录,在将记录加载到listbox中。双击listbox中查询到的记录,再传递到testbox中显示。 希望能给...
这是关于listbox的一个应用小技巧,里面附有源码程序。
JQuery、C#两种操作ListBox的方法代码
现在在C#中处理有关的拖放操作变得比以前方便多了,现在就已一个例子说明,这个例子演示了通过鼠标的拖动在一个ListBox中进行排序操作。
Q759361 v2 C# winforms listbox 双击条目直接修改 窗体传值实时同步
文件功能描述: 对ListBox进行了拓展,实现了两个方面的功能 1:增加了每项的高度,原版默认每行的高度太小,外观不好看 2:增加了对项的双击事件
C# WinForm 用两个listBox之间的Item互相拖动 京华志&精华志出品 希望大家互相学习,互相进步 支持CSDN 支持微软 主要包括C# ASP.NET SQLDBA 源码 毕业设计 开题报告 答辩PPT等
C#中实现两个listBox控件之间传递数据的源代码
压缩文档里包含了所有代码及一个ListBox的拖放Demo,使用.Net4.0,.Net3.5下已知的问题是DependencyObject类没有SetCurrentValue方法,可以使用SetValue方法代替。 项目是使用的VS2012创建的,若无法打开,请自行...
一个C#的ListBox简单应用例子。 实现在ListBox控件间进行数据交换。
此文档提供了一个委托方法,来控制访问listbox,用于多线程操作。c#
在ListBox间拖放选项.rar在ListBox间拖放选项.rar
c# Listbox自动滚动
ListBox 项的双击事件 ListBox 项的双击事件
在C#中使用ListBox控件实例源码在
c# 重写的listbox 有比较好看的样式