0

我打算在我的以下代码中放置一个计时器,以便在 5 秒后再次启用该按钮。如您所见,用户发送 5 条消息后,我的发送按钮将被禁用。我想在 5 秒后启用它。

欢迎任何建议。

public bool stopSpam(int counter)
    {
        int spam = counter;

        if (spam < 6)
        {
            return false;
        }
        else
        {
            return true;
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        counter++;

        bool check = stopSpam(counter);
        if (check == false)
        {
            if (textBox2.Text != "")
            {
                if (textBox2.Text.ToLower().StartsWith("/"))
                {
                    onCommand(textBox2.Text);
                    string datetimestring = string.Format("{0:yyyy-MM-dd_hh-mm-ss-tt}.txt", DateTime.Now);
                    String exePath = string.Format(Application.StartupPath + "\\logs\\" + "msglogs {0}", datetimestring);
                    StreamWriter writer = File.CreateText(exePath);
                    writer.Write(textBox1.Text);
                    writer.Close();
                    textBox2.Text = "";
                }
                else
                {
                    m_ChildConnection.SendMessage("MSG :" + textBox2.Text);
                    string datetimestring = string.Format("{0:yyyy-MM-dd_hh-mm-ss-tt}.txt", DateTime.Now);
                    String exePath = string.Format(Application.StartupPath + "\\logs\\" + "msglogs {0}", datetimestring);
                    StreamWriter writer = File.CreateText(exePath);
                    writer.Write(textBox1.Text);
                    writer.Close();
                    textBox2.Text = "";
                }
            }
        }
        else
        {
            button1.Enabled = false;
        }

提前感谢!

4

3 回答 3

2

有一个计时器,将其间隔设置为 5 秒(5000)。默认情况下保持禁用。

按下按钮时启用计时器

private void button1_Click(object sender, EventArgs e)
{
    button1.Enabled = false;
    timer.Enabled = true;
}

当 5 秒后出现滴答声时,启用按钮并再次禁用计时器。

private void timer_Tick(object sender, EventArgs e)
{
    button1.Enabled = true;
    timer.Enabled = false;
}
于 2012-07-25T03:39:25.380 回答
1

很难弄清楚您要达到的目标,但您可以采取以下步骤在 5 秒后禁用启用按钮。

添加:

private Timer t;

作为类变量。

然后在 InitializeComponent 之后添加:

t = new Timer(5000){Enabled = false, Tick += (myTick)};

然后添加这个方法:

private void myTick(object source, ElapsedEventArgs e)
{
    button1.Enabled = true;
}

另外,请考虑更新此方法:

您的 stopSpam 方法:

public bool stopSpam(int counter)
{
   return counter >= 6;
}

其实这个方法其实是不需要的:

简单地改变

if(check == false)

if(counter > 5)
于 2012-07-25T04:02:44.303 回答
0

您可以简单地使用System.Timers.Timer类来放置计时器。

//Define the timer 
private System.Timers.Timer buttonTimer;

// Initialize the timer with a five second interval.
buttonTimer= new System.Timers.Timer(5000);

// Hook up the Elapsed event for the timer.
buttonTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);

//Start the timer
buttonTimer.Start();

// Enable the button in timer elapsed event handler
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
    button1.Enabled = true;
}
于 2012-07-25T03:46:27.330 回答