C#-创建自定义双击事件
2023-09-11 14:17:10 时间
.NET Compact Framework 不支持按钮的 Windows 窗体 DoubleClick 事件。但是您可以创建一个从 Button 类派生的控件来实现该事件。
创建自定义双击事件
-
创建一个从 System.Windows.Forms.Button 类派生的类。
-
声明一个 DoubleClick 事件。
-
使用代码重写 OnClick 方法,以在指定时间内单击按钮时引发 DoubleClick 事件。
示例:
此示例创建一个 DoubleClickButton 自定义控件并在一个窗体上实现该控件。
using System; using System.Windows.Forms; using System.Drawing; namespace ButtonDClick { public class Form1 : System.Windows.Forms.Form { // Track the number of // double-clicks with the count variable. int count = 0; public Form1() { InitializeComponent(); // Display OK button for closing. this.MinimizeBox = false; // Create an instance of the DoubleClickButton class. DoubleClickButton dClickB = new DoubleClickButton(); dClickB.Bounds = new Rectangle(10,10,200,30); dClickB.Text = "Double-click me!"; Controls.Add(dClickB); // Add the DClick event hander to the DoubleClick event. dClickB.DoubleClick += new EventHandler(DClick); } protected override void Dispose( bool disposing ) { base.Dispose( disposing ); } private void InitializeComponent() { this.Text = "Form1"; } private void DClick(object o, EventArgs e) { // Display the number of double-clicks. MessageBox.Show("Double-click count = " + ++count); } static void Main() { Application.Run(new Form1()); } // Derive a button with extended funtionality // from the Button class. public class DoubleClickButton : System.Windows.Forms.Button { // Note that the DoubleClickTime property gets // the maximum number of milliseconds allowed between // mouse clicks for a double-click to be valid. int previousClick = SystemInformation.DoubleClickTime; public new event EventHandler DoubleClick; protected override void OnClick(EventArgs e) { int now = System.Environment.TickCount; // A double-click is detected if the the time elapsed // since the last click is within DoubleClickTime. if ( now - previousClick <= SystemInformation.DoubleClickTime) { // Raise the DoubleClick event. if (DoubleClick != null) DoubleClick(this,EventArgs.Empty); } // Set previousClick to now so that // subsequent double-clicks can be detected. previousClick = now; // Allow the base class to raise the regular Click event. base.OnClick(e); } // Event handling code for the DoubleClick event. protected new virtual void OnDoubleClick(EventArgs e) { if (this.DoubleClick != null) this.DoubleClick(this, e); } } } }
相关文章
- C#-FTP
- c#代码 天气接口 一分钟搞懂你的博客为什么没人看 看完python这段爬虫代码,java流泪了c#沉默了 图片二进制转换与存入数据库相关 C#7.0--引用返回值和引用局部变量 JS直接调用C#后台方法(ajax调用) Linq To Json SqlServer 递归查询
- C#中缓存的使用 ajax请求基于restFul的WebApi(post、get、delete、put) 让 .NET 更方便的导入导出 Excel .net core api +swagger(一个简单的入门demo 使用codefirst+mysql) C# 位运算详解 c# 交错数组 c# 数组协变 C# 添加Excel表单控件(Form Controls) C#串口通信程序
- C# DateTime的11种构造函数 [Abp 源码分析]十五、自动审计记录 .Net 登陆的时候添加验证码 使用Topshelf开发Windows服务、记录日志 日常杂记——C#验证码 c#_生成图片式验证码 C# 利用SharpZipLib生成压缩包 Sql2012如何将远程服务器数据库及表、表结构、表数据导入本地数据库
- 服务器文档下载zip格式 SQL Server SQL分页查询 C#过滤html标签 EF 延时加载与死锁 在JS方法中返回多个值的三种方法(转载) IEnumerable,ICollection,IList接口问题 不吹不擂,你想要的Python面试都在这里了【315+道题】 基于mvc三层架构和ajax技术实现最简单的文件上传 事件管理
- 常量,字段,构造方法 调试 ms 源代码 一个C#二维码图片识别的Demo 近期ASP.NET问题汇总及对应的解决办法 c# chart控件柱状图,改变柱子宽度 使用C#创建Windows服务 C#服务端判断客户端socket是否已断开的方法 线程 线程池 Task .NET 单元测试的利剑——模拟框架Moq
- 微信通过openID发送消息/后台post、get提交并接收数据 C# .NET 配置404,500等错误信息 连接字符串
- C# 字符串拼接性能探索 c#中+、string.Concat、string.Format、StringBuilder.Append四种方式进行字符串拼接时的性能
- C#学习记录——密封类与密封方法
- c#重载运算符
- C#【必备技能篇】Hex文件转bin文件的代码实现
- C#,子集和问题(Subset Sum Problem)的算法与源代码
- C#,最大公约数(GCD)斯坦因(Stein)算法的源代码
- C#中控件Control的Paint事件和OnPaint虚函数的区别
- 用c#开发微信 (4) 基于Senparc.Weixin框架的接收事件推送处理 (源码下载)
- 《C#零基础入门之百识百例》(七十八)委托事件实例练习3 -- 观察者模式
- C#查询数组中是否存在某元素(转载)
- 为C#自定义控件添加自定义事件
- C#操作剪切板(Clipboard)
- 【C#/WPF】窗体定时自动关闭