.NET Compact Framework 不支持按钮的 Windows 窗体 DoubleClick 事件。但是您可以创建一个从 Button 类派生的控件来实现该事件。

创建自定义双击事件

  1. 创建一个从 System.Windows.Forms.Button 类派生的类。

  2. 声明一个 DoubleClick 事件。

  3. 使用代码重写 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#-创建自定义双击事件的更多相关文章

  1. [前端][自定义DOM事件]不使用setTimeout实现双击事件或n击事件

    使用setTimeout实现双击事件 例如,这样: let div = document.getElementById("div"); doubleClick(div, funct ...

  2. bootstrap-treeview 自定义实现双击事件

    bootstrap-treeview是一款效果非常酷的基于bootstrap的jQuery多级列表树插件.该jQuery插件基于Twitter Bootstrap,以简单和优雅的方式来显示一些继承树结 ...

  3. 细说WPF自定义路由事件

    WPF中的路由事件 as U know,和以前Windows消息事件区别不再多讲,这篇博文中,将首先回顾下WPF内置的路由事件的用法,然后在此基础上自定义一个路由事件. 1.WPF内置路由事件   W ...

  4. WPF:自定义路由事件的实现

    路由事件通过EventManager,RegisterRoutedEvent方法注册,通过AddHandler和RemoveHandler来关联和解除关联的事件处理函数:通过RaiseEvent方法来 ...

  5. WPF自定义RoutedEvent事件示例代码

    ************************* 引用网友,便于查找所用..... 创建自定义路由事件和应用分为6个步骤: (1)自定义路由事件参数对象 (2)声明并注册路由事件 (3)为路由事件添 ...

  6. WPF 自定义路由事件

    如何:创建自定义路由事件 首先自定义事件支持事件路由,需要使用 RegisterRoutedEvent 方法注册 RoutedEvent C#语法 public static RoutedEvent ...

  7. Wpf自定义路由事件

    创建自定义路由事件大体可以分为三个步骤: ①声明并注册路由事件. ②为路由事件添加CLR事件包装. ③创建可以激发路由事件的方法. 以ButtonBase类中代码为例展示这3个步骤: public a ...

  8. WPF自学入门(四)WPF路由事件之自定义路由事件

    在上一遍博文中写到了内置路由事件,其实除了内置的路由事件,我们也可以进行自定义路由事件.接下来我们一起来看一下WPF中的自定义路由事件怎么进行创建吧. 创建自定义路由事件分为3个步骤: 1.声明并注册 ...

  9. WPF自定义路由事件(二)

    WPF中的路由事件 as U know,和以前Windows消息事件区别不再多讲,这篇博文中,将首先回顾下WPF内置的路由事件的用法,然后在此基础上自定义一个路由事件. 1.WPF内置路由事件 WPF ...

随机推荐

  1. 彻底搞清js中闭包(Closure)的概念

    js中闭包这个概念对于初学js的同学来说, 会比较陌生, 有些难以理解, 理解起来非常模糊. 今天就和大家一起来探讨一下这个玩意. 相信大家在看完后, 心中的迷惑会迎然而解. 闭包概念: 闭包就是有权 ...

  2. 数论/the second wave

    扩展欧几里得算法. void exgcd(int a,int b,int&x,int&y){ if(!b) { x=1;y=0;return ; } exgcd(b,a%b,x,y); ...

  3. 信息学院第九届ACM程序设计竞赛题解

     A: 信号与系统 Time Limit: 1000 MS Memory Limit: 65536 KBTotal Submit: 238 Accepted: 44 Page View: 69 Des ...

  4. log4net 将日志写入数据库

    asp.net利用log4net写入日志到SqlServer数据库,Log4net是一个开源的错误日志记录项目,易用性强,源自log4j,品质值得信赖. 下面就我的安装部署log4net到MS sql ...

  5. [selenium webdriver Java]隐式的等待同步

    Selenium WebDriver提供了隐式等待来同步测试.当使用了隐式等待执行测试的时候,如果WebDriver没有在DOM中找到元素,将继续等待,超出设定时间后,抛出找不到元素异常 即,当元素没 ...

  6. ubuntu下安装JDK详解

    码农博客 即将到期,现将博客中部分文章转载到博客园.本文发表与2012年,转载时略有删减 安装JDK其实只要搞定两个问题,安装目录以及配置文件.如果你只想要快速安装JDK,请略过此部分直接看安装篇. ...

  7. 【hdu3065】病毒侵袭持续中

    题意: 求目标串中每个模式串出现几次 目标串长度<=2000000 模式串<=1000个 模式串长度<=50 题解: 这不就是AC自动机的模板题吗! 求fail树中模式串的子树中有几 ...

  8. Apache下安装配置mod_pagespeed模块,轻松完成网站提速

    mod_pagespeed是一个开源的Apache module,它由谷歌开发,通过优化你的网页来减少响应延迟和带宽占用.作用参考ngx_pagespeed功能:http://blog.linuxey ...

  9. Apache Hadoop压缩包与Eclipse结合,导入jar包归整总结(手动)

    *************************  有些,是没必要全导入的.以后到工作了,用Maven,就自动会导入其中一些.************************ 一般,工作中,用的更多 ...

  10. Android问题-selection contains a component,button7,introduced in an ancestor and cannot be deleted.

    问题现象: 在开发Android时增加的控件想删除,可是删除时提示“Android问题-selection contains a component,button7,introduced in an ...