分享

C#使用System.Timers.Timer类实现一个Button锁定功能(windows form)

 昵称10504424 2013-02-20
  1. public partial class Form1 : Form  
  2.    {  
  3.        public Form1()  
  4.        {  
  5.            InitializeComponent();  
  6.            Control.CheckForIllegalCrossThreadCalls = false;//消除异常:Cross-thread operation not valid:Control 'button1' accessed from a thread other than the thread it was created on .   
  7.        }  
  8.        System.Timers.Timer t = new System.Timers.Timer(5000);  
  9.         
  10.        private void button1_Click(object sender, EventArgs e)  
  11.        {  
  12.            MessageBox.Show("This Button has been locked");  
  13.            t.Start();  
  14.            t.AutoReset = false;  
  15.            t.Enabled = true;  
  16.            button1.Enabled = false;  
  17.            t.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);  
  18.              
  19.        }  
  20.        private void  timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)  
  21.        {  
  22.            t.Stop();  
  23.            this.button1.Enabled = true;//必须加上Control.CheckForIllegalCrossThreadCalls = false;否则报异常:Cross-thread operation not valid:Control 'button1' accessed from a thread other than the thread it was created on .   
  24.        }  
  25.        private void button2_Click(object sender, EventArgs e)  
  26.        {  
  27.            MessageBox.Show(Owner,"OK");  
  28.        }  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多