希望按钮在单击后禁用30秒并自动启用 [英] Want a button to disable for 30 second after click and enable it automatically

查看:151
本文介绍了希望按钮在单击后禁用30秒并自动启用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个项目上,这个项目需要从random.org中随机获取16个字节.我可以从网站上获取它,并将这16个字节写入textedit.我也有一个刷新按钮.每次单击此按钮时,都会出现新的16字节并将其写入textedit中.

I am on a project and this project needs to take randomly 16 bytes from random.org. I can take it from website and write this 16 bytes to a textedit. I also have a refresh button. Everytime this button is clicked, new 16 byte comes and it is written in textedit.

我想防止用户顺序单击刷新按钮.我想要的是单击后禁用刷新按钮.但是,我想做的另一件事是在30秒后自动启用此按钮.

I want to prevent the user to click refresh button sequentially. What I want is to disable refresh button after it is click. However, another thing I want is to enable this button after 30 seconds automatically.

我在按钮单击事件中尝试了thread.sleep(30000),但是它使整个程序停止了30秒钟.我只想禁用刷新按钮30秒钟,而不要禁用程序的其余部分.

I've tried thread.sleep(30000) in the button click event but it stops whole program for 30 seconds. I want to disable just refresh button for 30 seconds, not the rest of the program.

我正在考虑向用户展示一种秒表,以便他们可以看到需要等待多长时间的下一次点击.例如,当他们单击刷新"按钮后,就会出现此30秒秒表.

I am thinking to show to the users kind of stopwatch so that they can see how much time they should wait for next click. For example, after they click refresh button, there appears this 30 seconds stopwatch.

推荐答案

尝试如下操作:

System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
private void button1_Click(object sender, EventArgs e)  // event handler of your button
{                
    timer.Interval = 30000; // here time in milliseconds
    timer.Tick += timer_Tick;
    timer.Start();
    button1.Enabled = false;

    // place get random code here
}

void timer_Tick(object sender, System.EventArgs e)
{
    button1.Enabled = true;
    timer.Stop();
}

这篇关于希望按钮在单击后禁用30秒并自动启用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆