C#-创建自定义双击事件
.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 = ; 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(,,,);
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#-创建自定义双击事件的更多相关文章
- [前端][自定义DOM事件]不使用setTimeout实现双击事件或n击事件
		使用setTimeout实现双击事件 例如,这样: let div = document.getElementById("div"); doubleClick(div, funct ... 
- bootstrap-treeview 自定义实现双击事件
		bootstrap-treeview是一款效果非常酷的基于bootstrap的jQuery多级列表树插件.该jQuery插件基于Twitter Bootstrap,以简单和优雅的方式来显示一些继承树结 ... 
- 细说WPF自定义路由事件
		WPF中的路由事件 as U know,和以前Windows消息事件区别不再多讲,这篇博文中,将首先回顾下WPF内置的路由事件的用法,然后在此基础上自定义一个路由事件. 1.WPF内置路由事件 W ... 
- WPF:自定义路由事件的实现
		路由事件通过EventManager,RegisterRoutedEvent方法注册,通过AddHandler和RemoveHandler来关联和解除关联的事件处理函数:通过RaiseEvent方法来 ... 
- WPF自定义RoutedEvent事件示例代码
		************************* 引用网友,便于查找所用..... 创建自定义路由事件和应用分为6个步骤: (1)自定义路由事件参数对象 (2)声明并注册路由事件 (3)为路由事件添 ... 
- WPF 自定义路由事件
		如何:创建自定义路由事件 首先自定义事件支持事件路由,需要使用 RegisterRoutedEvent 方法注册 RoutedEvent C#语法 public static RoutedEvent ... 
- Wpf自定义路由事件
		创建自定义路由事件大体可以分为三个步骤: ①声明并注册路由事件. ②为路由事件添加CLR事件包装. ③创建可以激发路由事件的方法. 以ButtonBase类中代码为例展示这3个步骤: public a ... 
- WPF自学入门(四)WPF路由事件之自定义路由事件
		在上一遍博文中写到了内置路由事件,其实除了内置的路由事件,我们也可以进行自定义路由事件.接下来我们一起来看一下WPF中的自定义路由事件怎么进行创建吧. 创建自定义路由事件分为3个步骤: 1.声明并注册 ... 
- WPF自定义路由事件(二)
		WPF中的路由事件 as U know,和以前Windows消息事件区别不再多讲,这篇博文中,将首先回顾下WPF内置的路由事件的用法,然后在此基础上自定义一个路由事件. 1.WPF内置路由事件 WPF ... 
随机推荐
- Hive QL 介绍
			小结 本次课程学习了 Hive QL 基本语法和操作. 一.实验环境说明 1. 环境登录 无需密码自动登录,系统用户名shiyanlou,密码shiyanlou 2. 环境介绍 本实验环境采用带桌面的 ... 
- maven常用插件配置详解
			常用插件配置详解Java代码 <!-- 全局属性配置 --> <properties> <project.build.name>tools</proje ... 
- hibernate实体的几种状态:
			hibernate实体的几种状态: 实体的生命周期中,实体主要经过瞬时(Transient),托管(Attatched或Managed),游离(Detached)和销毁(Removed)四个状态. 瞬 ... 
- 批量产生ssh2项目中hibernate带注解的pojo类的快捷方法
			近几个月一直在忙于项目组的ios应用项目的开发,没有太多时间去研究web应用方面的问题了.刚好,昨天有网友问到如何批量产生hibernate带注解的pojo类的快捷方法,所谓批量就是指将当前数据库中所 ... 
- 陈正冲老师对于c语言野指针的解释
			那到底什么是野指针呢?怎么去理解这个“野”呢?我们先看别的两个关于“野”的词: 野孩子:没人要,没人管的孩子:行为动作不守规矩,调皮捣蛋的孩子.野狗:没有主人的狗,没有链子锁着的狗,喜欢四处咬人. 对 ... 
- 【转】parallels desktop 11 授权许可文件删除方法
			原文网址:http://www.macappstore.net/tips/parallels-desktop-uninstall/ 很多同学在安装parallels desktop 11破解版后显示还 ... 
- HDU 3007  Buried memory & ZOJ 1450  Minimal Circle
			题意:给出n个点,求最小包围圆. 解法:这两天一直在学这个神奇的随机增量算法……看了这个http://soft.cs.tsinghua.edu.cn/blog/?q=node/1066之后自己写了好久 ... 
- [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.4.5
			Suppose it is known that $\scrM$ is an invariant subspace for $A$. What invariant subspaces for $A\o ... 
- 【JMeter】JMeter完成一个java请求的压测
			先定义一下我说的remoteService:即远程调用服务,没有http的url.不对外提供或者对外提供有限的服务.具体视各公司的代码架构所定,比如有些公司为web工程,scf服务,db.scf即为服 ... 
- unity3d自己写角色移动脚本
			废话没有,直接上代码: using UnityEngine; using System.Collections; public class SuperWalk : MonoBehaviour { pu ... 
