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 ...
随机推荐
- 1439. Battle with You-Know-Who(splay树)
1439 路漫漫其修远兮~ 手抄一枚splay树 长长的模版.. 关于spaly树的讲解 网上很多随手贴一篇 貌似这题可以用什么bst啦 堆啦 平衡树啦 等等 这些本质都是有共同点的 查找.删除特 ...
- 1350. Canteen(map)
1350 这题没什么 就考一下map的用法吧 #include <iostream> #include<cstdio> #include<cstring> #in ...
- 转: Linux 技巧:让进程在后台可靠运行的几种方法
我们经常会碰到这样的问题,用 telnet/ssh 登录了远程的 Linux 服务器,运行了一些耗时较长的任务, 结果却由于网络的不稳定导致任务中途失败.如何让命令提交后不受本地关闭终端窗口/网络断开 ...
- Codeforces Round #221 (Div. 2) C. Divisible by Seven(构造 大数除法 )
上几次的一道cf题. 题目:http://codeforces.com/contest/376/problem/C 性质: (4)a与b的和除以c的余数(a.b两数除以c在没有余数的情况下除外),等于 ...
- JSOI2015 R3 退队滚粗了
JSTSC最终落下帷幕,最终还是没能翻盘成功——退队了,遗憾啊,中原得鹿不由人 day0 没啥好说的,我一开始把省常中和常州一中搞混了……,不过常州一中的伙食还是相当良心的,比省常中好 考前感觉状态不 ...
- 各种好用的工具之一 ---- PNGGauntlet
1.PNGGauntlet实际上是一个图形前端,压缩图像的过程中使用的是PNGOUT, OptiPNG, 和DeflOpt这三款软件. 软件官网:http://pnggauntlet.com/ 可自行 ...
- 将archlinux 2013-06-01版,安装配置为个人工作站
本文安装所使用的镜像为:archlinux-2013.06.01-dual.iso.首先请看看我安装完成之后的效果.图一,是第一个虚拟桌面及右键菜单图: 图二,是第二个虚拟桌面效果图.后几个虚拟桌面图 ...
- [转] 字符串模式匹配算法——BM、Horspool、Sunday、KMP、KR、AC算法一网打尽
字符串模式匹配算法——BM.Horspool.Sunday.KMP.KR.AC算法一网打尽 转载自:http://dsqiu.iteye.com/blog/1700312 本文内容框架: §1 Boy ...
- EntityFramework程序集版本不同
问题: Normal 0 7.8 磅 0 2 false false false EN-US ZH-CN X-NONE /* Style Definitions */ table.MsoNormalT ...
- 【js与jquery】电子邮箱、手机号、邮政编码的正则验证
//验证邮政编码 $("#postcode").blur(function(){ //获取邮政编码 var postcode=$("#postcode").va ...