C#Windows窗体处理单击垃圾邮件 [英] C# windows forms handle click spam

查看:59
本文介绍了C#Windows窗体处理单击垃圾邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含许多用户控件和每个按钮的应用程序,每个按钮都有一个OnClick事件,该事件会做一些重要的事情,然后发送给新的用户控件.

I have an app with many user controls and many buttons on each, each button has an OnClick event which does some important stuff and then sends to a new user control.

问题出在用户真正快速单击多次时,事件代码在退出到新的用户控件之前被执行了多次,从而引起了问题.

The problem comes when the user clicks really fast multiple times, the event code gets executed more than once before exitting to a new user control, causing problems.

我当前的解决方案是在事件处理程序的第一行禁用按钮,但是对每个窗口和处理程序执行此操作都会很麻烦,我该怎么办?

My current solution is to disable the button on the very first line of the event handler, but doing this to every window and handler would be troublesome, what can I do?

从Button派生并重写OnClick事件是否是一个好的解决方案,因此它始终会检查工作"变量,如果为true,则不会启动该事件?像这样:

Would it be a good solution to derive from Button, and override the OnClick event so it always does a check for a "working" variable, and if it is true, it doesnt start the event ? Something like:

public class MyButton : Button
{
private static bool isWorking = false;

protected override void OnClick(EventArgs e)
{
    if (!isWorking)
    {
        isWorking = true;
        base.OnClick(e);
        isWorking = false;
    }
    //Else do nothing
}

}

推荐答案

您可以使用一些 timeStamp 来延迟两次单击之间的时间:

You can use some timeStamp to delay between 2 clicks:

DateTime timeStamp;
//this will handle the clicks with the allowed interval being 0.5 second
//Note that a tick is equal to 1/10,000,000 of second.
private void click_Handler(object sender, EventArgs e) {
   if ((DateTime.Now - timeStamp).Ticks < 5000000) return;
   timeStamp = DateTime.Now;
   //your code goes here ....
}

这篇关于C#Windows窗体处理单击垃圾邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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