Chaud
Chaud

Reputation: 1

Setting a timer to an event

I intend to put a timer in my the following code so that that the button will be enabled again after 5 seconds. AS you can see, my send button will be disabled after the user send 5 message. I want to enabled it after 5 seconds have elapsed.

Any suggestion is welcomed.

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;
        }

Thanks in adavence!

Upvotes: 0

Views: 1513

Answers (3)

Chibueze Opata
Chibueze Opata

Reputation: 10054

Hard to figure out what you're trying to achieve but you could take the following steps to disable the enable the button after 5 seconds.

Add:

private Timer t;

as a class variable.

then after your InitializeComponent add:

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

then add this method:

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

Also, consider updating this method:

your stopSpam method to:

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

In fact, there is actually no need for the method:

Simply change

if(check == false)

to

if(counter > 5)

Upvotes: 1

ABH
ABH

Reputation: 3439

You can simply use System.Timers.Timer class to put 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;
}

Upvotes: 0

nunespascal
nunespascal

Reputation: 17724

Have a timer, set its interval to 5 seconds (5000). Keep it disabled by default.

When the button is pressed enable the timer

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

When a tick occurs after 5 seconds, enable the button and disable the timer again.

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

Upvotes: 2

Related Questions