zoukankan      html  css  js  c++  java
  • C#-创建自定义双击事件

    .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 = 0;
    
            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(10,10,200,30);
                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);
                }
            }
        }
    }
  • 相关阅读:
    ajax打开新窗口防止被浏览器阻止问题
    网站安全
    纠结点
    haven单词5个
    jQuery 鼠标停留样式
    js new date
    XML解析特殊字符出错
    个人对JQuery Proxy()函数的理解
    sql server 数据库 杀掉死锁进程
    转载一个:【C#4.0】中的dynamic与var的区别西南烟雨
  • 原文地址:https://www.cnblogs.com/dekevin/p/3614549.html
Copyright © 2011-2022 走看看