首页 / C# / C#-创建自定义双击事件
C#-创建自定义双击事件
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了C#-创建自定义双击事件,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2383字,纯文字阅读大概需要4分钟。
内容图文
.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); } protectedoverridevoid Dispose( bool disposing ) { base.Dispose( disposing ); } privatevoid InitializeComponent() { this.Text = "Form1"; } privatevoid DClick(object o, EventArgs e) { // Display the number of double-clicks. MessageBox.Show("Double-click count = " + ++count); } staticvoid Main() { Application.Run(new Form1()); } // Derive a button with extended funtionality // from the Button class.publicclass 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; publicnewevent EventHandler DoubleClick; protectedoverridevoid 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.protectednewvirtualvoid OnDoubleClick(EventArgs e) { if (this.DoubleClick != null) this.DoubleClick(this, e); } } } }
原文:http://www.cnblogs.com/dekevin/p/3614549.html
内容总结
以上是互联网集市为您收集整理的C#-创建自定义双击事件全部内容,希望文章能够帮你解决C#-创建自定义双击事件所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。