惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

PCI Perspectives
PCI Perspectives
AI
AI
L
LINUX DO - 最新话题
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
W
WeLiveSecurity
T
Troy Hunt's Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Forbes - Security
Forbes - Security
Google DeepMind News
Google DeepMind News
Hacker News - Newest:
Hacker News - Newest: "LLM"
O
OpenAI News
D
DataBreaches.Net
S
Secure Thoughts
SecWiki News
SecWiki News
L
LangChain Blog
Google DeepMind News
Google DeepMind News
博客园 - 【当耐特】
Recent Announcements
Recent Announcements
Martin Fowler
Martin Fowler
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Engineering at Meta
Engineering at Meta
H
Heimdal Security Blog
有赞技术团队
有赞技术团队
The Last Watchdog
The Last Watchdog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
宝玉的分享
宝玉的分享
N
News | PayPal Newsroom
博客园 - 聂微东
TaoSecurity Blog
TaoSecurity Blog
Cloudbric
Cloudbric
Blog — PlanetScale
Blog — PlanetScale
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
量子位
T
Threatpost
Security Latest
Security Latest
P
Privacy & Cybersecurity Law Blog
GbyAI
GbyAI
博客园 - 叶小钗
L
Lohrmann on Cybersecurity
S
Security @ Cisco Blogs
Y
Y Combinator Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Tailwind CSS Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Apple Machine Learning Research
Apple Machine Learning Research
N
Netflix TechBlog - Medium
L
LINUX DO - 热门话题

博客园 - 随风逝去(叶进)

MySQL分表(Partition)学习研究报告 用WPF+MongoDB开发房产信息收集器(4)——房产信息采集器总体介绍附程序下载 用WPF+MongoDB开发房产信息收集器(3)——MongoDB入门 用WPF+MongoDB开发房产信息收集器(2)——后台线程 用WPF+MongoDB开发房产信息收集器(1) 解决FireFox不能Debug Silverlight程序的问题 - 随风逝去(叶进) - 博客园 终于可以继续往下了! 回首2008 另类的二级域名实现方法 触发C#Button的双击事件 到底要不要去面试? 【C#食谱】【面食】菜单7:用默认值初始化泛型变量 类和对象 【C#食谱】【杭帮菜】菜单2:写一个TCP客户端 C#Winform下用正则表达式限制TextBox只能输入数字 【C#食谱】【杭帮菜】菜单1:写一个TCP服务端 【C#食谱】【川菜】菜单1:列出被引用的程序集 实现需求工程的成功方法 实现需求工程的成功方法——难度:高;影响:低
【更新 2008.10.16】触发C#Button的双击事件
随风逝去(叶进) · 2008-10-16 · via 博客园 - 随风逝去(叶进)

【更新 2008.10.16】 根据各位网友的意见,对双击拓展按钮做了更改,修复了两个个Bug:

1. 即使两次点击时间间隔较长也会触发双击事件。

2. 修复这样一个Bug:先点一下,接着连续点两下,不能触发双击事件的Bug;传说中的狐狸

在C#中,Button是有DoubleClick事件的,只是它没有出现在事件列表中,而且.net也是把这个事件给"屏蔽"掉了,无论你双击一个按钮怎么快,也不会触发这个事件。

为了能够触发Button的双击事件,我们可以重载MouseDown事件,记录每次鼠标按下的时间,如果两次点击的时间足够近的话,就触发一次双击方法。

这样也大致能够达到双击的效果,我所采用的方法其实和这个类似,是继承Button类,重载Button类的Click事件,在Click事件中判断两次点击的时间,如果近的话,就触发双击事件。废话少说,上代码:


//======================================================================
//
//        Copyright (C) 2007-2008 杭州石鼓文信息科技有限公司    
//        All rights reserved
//
//        filename :ButtonEx
//        description :
//
//        created by 叶进 at  10/13/2008 15:38:24
//        http://adaiye.cnblogs.com
//
//======================================================================

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;public class ButtonEx : Button
    {
        
public new event EventHandler DoubleClick;

        DateTime clickTime;

bool isClicked = false;protected override void OnClick(EventArgs e)
        {
            
base.OnClick(e);if (isClicked)
            {
                TimeSpan span 
= DateTime.Now - clickTime;

                if (span.TotalMilliseconds < SystemInformation.DoubleClickTime)  

//  把milliseconds改成totalMilliseconds 因为前者不是真正的时间间隔,totalMilliseconds才是真正的时间间隔

                {
                    DoubleClick(
this, e);
                    isClicked 
= false;
                }
            }
            
else
            {
                isClicked 
= true;
                clickTime 
= DateTime.Now;
            }
        }
    }

这样,就可以为创建的ButtonEx按钮添加DoubleClick事件了:

button.DoubleClick += new EventHandler(Button_DoubleClick);  // 双击按钮事件

另外,根据传说中的狐狸意见,去掉isClicked,修复了另一个bug:先点一下,接着连续点两下,不能触发双击事件

改进后的代码如下:

Code

另外,还有很多问题,需要改进,比如说 Anders Liu提出的:两次单击的位置还必须限制在一个范围里,才能形成双击,这里怎么记录下鼠标点击的位置?

 Anders06: 触发了双击事件能不能把前面两次已触发的单击事件给吞掉?

所以,希望各位同仁多多提供帮助,意见或建议!

特别鸣谢,谢谢你们提供宝贵的意见和建议:

Anders Liu

Anders06

传说中的狐狸