wpf 想要一个按钮在点击后禁用 30 秒并自动启用它
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20717982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Want a button to disable for 30 second after click and enable it automatically
提问by Meri? AKGüL
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.
我在做一个项目,这个项目需要从 random.org 中随机获取 16 个字节。我可以从网站上获取它并将这 16 个字节写入文本编辑器。我也有一个刷新按钮。每次单击此按钮时,都会出现新的 16 字节并写入 textedit。
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.
我想阻止用户依次单击刷新按钮。我想要的是在单击后禁用刷新按钮。但是,我想要的另一件事是在 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.
我thread.sleep(30000)在按钮点击事件中尝试过,但它会停止整个程序 30 秒。我只想禁用刷新按钮 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.
我想向用户展示一种秒表,这样他们就可以看到他们应该等待下一次点击的时间。比如点击刷新按钮后,就会出现这个30秒秒表。
回答by Ilya
If we are talking about WPF here, there is a way to achieve the desired behavior with EventTriggers without any code-behind:
如果我们在这里谈论 WPF,则有一种方法可以使用 EventTriggers 实现所需的行为,而无需任何代码隐藏:
<Style TargetType="Button">
<Style.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="IsEnabled">
<DiscreteBooleanKeyFrame KeyTime="00:00:00" Value="False" />
<DiscreteBooleanKeyFrame KeyTime="00:00:30" Value="True" />
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Style.Triggers>
</Style>
回答by user2737037
Try something like this:
尝试这样的事情:
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();
}
回答by hollen9
Based on user2737037's answer, here's the annoumous code-behind approach.
根据 user2737037 的回答,这里是 annoumous 代码隐藏方法。
In this way, you don't have to declare any member fields, which keep your code clean.
通过这种方式,您不必声明任何成员字段,从而使您的代码保持整洁。
private void btnFoo_Click(object s, RoutedEventArgs e)
{
var btn = (Button)sender;
btn.IsEnabled = false; //Disable button.
var fooTimer = new System.Timers.Timer(5000); //Exceute after 5000 milliseconds
timer.Elapsed += (fooTimer_s, fooTimer_e) =>
{
//It has to be dispatched because of thread crossing if you are using WPF.
Dispatcher.Invoke(()=>
{
btn.IsEnabled = true; //Bring button back to life by enabling it.
fooTimer.Dispose();
});
};
fooTimer.Start();
//Insert your code here :)
}
回答by Yeldar Kurmangaliyev
Starting from C# 5.0 you can use async/awaitand Task.Delayto wait for 30 seconds in the middle of the method without blocking the UI thread:
从 C# 5.0 开始,您可以使用async/await并Task.Delay在方法中间等待 30 秒而不会阻塞 UI 线程:
private async void YourButton_Click(object sender, EventArgs e)
{
UIElement element = (UIElement)sender;
element.IsEnabled = false;
await Task.Delay(30000); // 30 seconds
element.IsEnabled = true;
}
回答by user3108818
try to use an timer an enable the button after a tick. For more information: http://www.dotnetperls.com/timer
尝试使用计时器并在滴答后启用按钮。更多信息:http: //www.dotnetperls.com/timer
回答by Sudhakar Tillapudi
System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();
timer1.Interval=1000;//one second
timer1.Tick += new System.EventHandler(timer1_Tick);
timer1.Start();
int count=30;
private void timer1_Tick(object sender, EventArgs e)
{
if(count!=0)
{
button1.Enabled=false;
label1.Text = count.ToString() +" seconds more to Enable Refresh Button";
count--;
}
else
{
button1.Enabled=true;
timer1.Stop();
}
}
回答by tire0011
use a timer there is a snippet to create the timer http://dotnet-snippets.de/snippet/timer-anlegen/111
使用计时器有一个片段来创建计时器 http://dotnet-snippets.de/snippet/timer-anlegen/111
in the on click in the button disable the button and start the timer. In the Tick enable the button and stop the timer.
在单击按钮时禁用按钮并启动计时器。在 Tick 中启用按钮并停止计时器。

