将计时器设置为事件

时间:2012-07-25 03:23:12

标签: c# winforms

我打算在我的下面的代码中放一个计时器,以便在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;
        }

感谢adavence!

3 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分: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)

答案 2 :(得分: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;
}