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 ...
随机推荐
- grep 同时满足多个关键字和满足任意关键字
grep 同时满足多个关键字和满足任意关键字 ① grep -E "word1|word2|word3" file.txt 满足任意条件(word1.word2和word ...
- @深入注解,在Java中设计和使用自己的注解
我们用过 JDK给我们提供的 @Override @Deprecated @SuppressWarning 注解 ,这些注解是JDK给我们提供的 ,我们只是在用别人写好的东西 ,那么我们是否可以 ...
- poj3275
比较笨啊,一直在想,到底问几次绝对能知道所有的关系呢? 后来看了题解才知道,问一次最少确定一对关系………… 这就好办le,n头牛有C(2,n)个关系 现在给出m条边,以确定的关系有多少呢?直接dfs啊 ...
- codevs3731 寻找道路
方向dfs判定是否可行,spfa跑最短路. noip水题,wa好几次. #include<cstdio> #include<algorithm> #include<cst ...
- TRSWCM学习问题总结
1,置标属性"id"是用来制定调用那个栏目的数据(全字配备,可以文字匹配好奇怪,好不专业.所以建议创建栏目或者站点的时候,将唯一标识设置成英文,这样才符合程序比对习惯) 2,需要添 ...
- JavaScript NodeList和Array
原文引用脚本之家作者:Jeff Wong,谢谢大神提供资源 在Web前端编程中,我们通常会通过document.getElementsByTagName或者document.getElementsBy ...
- apache开源项目--Jackrabbit
Apache Jackrabbit 是由 Apache Foundation 提供的 JSR-170 的开放源码实现.. 随着内容管理应用程序的日益普及,对用于内容仓库的普通.标准化 API 的需求已 ...
- mkimage工具 加载地址和入口地址 内核启动分析
第三章第二节 mkimage工具制作Linux内核的压缩镜像文件,需要使用到mkimage工具.mkimage这个工具位于u-boot-2013. 04中的tools目录下,它可以用来制作不压缩或者压 ...
- C# 有关文件路径的操作
1. 由文件全路径,获取文件扩展名.文件名等信息 string fullPath = @"\WebSite1\Default.aspx"; string filename = Sy ...
- SQL游标
最近工作中有用到游标,特简单总结一下: 一.简介 游标(Cursor)是处理数据的一种方法,为了查看或者处理结果集中的数据,游标提供了在结果集中一次以行或者多行前进或向后浏览数据的能力.我们 ...