.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. Windows CE,你妈吗喊你在多核上玩玩

    转自:http://hezongjian.com/blog/?p=9693 众所周知,CE的内核是完全重新写的,与9x根nt没有任何关系,这给CE带来了较好的实时性及灵活性.从诞生之日起,CE的内核改 ...

  2. poj2186Popular Cows(强连通分量)

    http://poj.org/problem?id=2186 用tarjan算出强连通分量的个数 将其缩点 连成一棵树  则题目所求即变成求出度为0 的那个节点 在树中是唯一的 即树根 #includ ...

  3. poj 1416 Shredding Company( dfs )

    我的dfs真的好虚啊……,又是看的别人的博客做的 题目== 题目:http://poj.org/problem?id=1416 题意:给你两个数n,m;n表示最大数,m则是需要切割的数. 切割m,使得 ...

  4. poj 2253 Frogger(最短路 floyd)

    题目:http://poj.org/problem?id=2253 题意:给出两只青蛙的坐标A.B,和其他的n-2个坐标,任一两个坐标点间都是双向连通的.显然从A到B存在至少一条的通路,每一条通路的元 ...

  5. 函数fsp_seg_inode_page_get_nth_inode

    #define FSEG_ARR_OFFSET (FSEG_PAGE_DATA + FLST_NODE_SIZE) #define FSEG_PAGE_DATA FIL_PAGE_DATA #defi ...

  6. PHP超大文件下载,断点续传下载

    源代码: <?php $sourceFile = "1.tmp"; //要下载的临时文件名 $outFile = "用户订单.xls"; //下载保存到客 ...

  7. Treeview控件的Node节点延迟加载

    Treeview控件是一个很常用的控件,用于展示资源或者组织结构的时候很方便,通常会在系统启动时进行资源的加载和节点目录的初始化,但在资源较多和层级较深的情况下,所有节点加载出来会耗费太多时间,影响体 ...

  8. (二)学习C#之内存管理

    一.当你运行你的程序通常都会访问哪些内存空间呢? 电脑自言自语道,“这个人要声明一个整数”或“这个人个方法”或“这个人要创建一个对象” 1.这些信息究竟是存在内存里的什么地方呢? 2.或者说用于描述这 ...

  9. Android学习的一些问题

    如何让Service常驻后台? 如何让App自启动? 如何让App自动更新? Handler Adapter Bundle Application getXXX()

  10. [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.2.7

    The set of all invertible matrices is a dense open subset of the set of all $n\times n$ matrices. Th ...