在网上找到一段代码        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.Milliseconds < SystemInformation.DoubleClickTime)
                {
                    DoubleClick(this, e);
                    isClicked = false;
                }
            }
            else
            {
                isClicked = true;
                clickTime = DateTime.Now;
            }
        }我用以下这段代码调用,没有任何反应Button btn = new Button();
btn.DoubleClick += new EventHandler(btn_DoubleClick);请高手指教

解决方案 »

  1.   

    你怎么用的代码。很明显这段代码要求你从Button继承一个类。然后你去用这个继承的button类,而不是原先的button。
      

  2.   

    using System;
    using System.Windows.Forms;namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();            MyButton myButton1 = new MyButton();
                this.Controls.Add(myButton1);            myButton1.DoubleClick += new EventHandler(btn_DoubleClick);
            }        private void btn_DoubleClick(Object sender, EventArgs e)
            {
                MessageBox.Show("btn_DoubleClick");
            }
        }    public class MyButton : Button
        {
            public new event EventHandler DoubleClick;
            DateTime clickTime;
            bool isClicked = false;
            protected override void OnClick(EventArgs e)
            {
                try
                {
                    base.OnClick(e);                if (isClicked)
                    {
                        TimeSpan span = DateTime.Now - clickTime;
                        if (span.Milliseconds < SystemInformation.DoubleClickTime)
                        {
                            DoubleClick(this, e);
                            isClicked = false;
                        }
                    }
                    else
                    {
                        isClicked = true;
            clickTime = DateTime.Now;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
        }
    }