Hi there! I've been looking everywhere around the internet and I couldn't find an exact code to help me create a working countdown timer in my WFA project.

What I want to do basically, is make it so that when after I click my button, the timer will count down like a few seconds then after the countdown is finished the button will continue on with the rest of the coding.

Thanks for any help in advance!

pilipino93
Reece

Drop a button and and a label on a form:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Timer = System.Timers.Timer;

namespace daniweb
{
  public partial class frmTimer : Form
  {
    private Timer timer;
    private const int requiredWaitTime = 10; //in seconds
    private DateTime dtStart;

    public frmTimer()
    {
      InitializeComponent();
      timer = new Timer();
      timer.Interval = (1000 * 1); //1 sec
      timer.AutoReset = true;
      timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
      this.Load += new EventHandler(frmTimer_Load);
    }

    void frmTimer_Load(object sender, EventArgs e)
    {
      dtStart = DateTime.Now;
      timer.Enabled = true;
    }

    public delegate void SetText(string s, bool EnableButton);
    public void SetMessageText(string s, bool EnableButton)
    {
      if (label1.InvokeRequired)
      {
        label1.Invoke(new SetText(SetMessageText), s, EnableButton);
      }
      else
      {
        label1.Text = s;
        button1.Enabled = EnableButton;
      }
    }

    void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
      int timeWaited = Convert.ToInt32(Math.Truncate(DateTime.Now.Subtract(dtStart).TotalSeconds));
      bool finished = (timeWaited >= requiredWaitTime);
      string msg;
      if (finished)
      {
        msg = "You're ready to go!";
      }
      else
      {
        msg = string.Format("Hold on for {0:F0} more seconds!", requiredWaitTime - timeWaited);
      }
      SetMessageText(msg, finished);
    }

  }
}

sknake I tried using your code, but I got some errors. I'll try to use DdoubleD's now =]

sknake I tried using your code, but I got some errors. I'll try to use DdoubleD's now =]

sknake, can you include the designer.cs portion?

Here

ugh, I used that link from double's post, I was doing everything fine, and it was working out okay until it told me i was getting errors so I was like wtf...

Isn't there a more simple way to do this. I don't know but like just having the timer and set the interval to like 5000 and start it, so that once button is clicked, itll count down 5 seconds and then continue?

ugh, I used that link from double's post, I was doing everything fine, and it was working out okay until it told me i was getting errors so I was like wtf...

Isn't there a more simple way to do this. I don't know but like just having the timer and set the interval to like 5000 and start it, so that once button is clicked, itll count down 5 seconds and then continue?

I've tried both the link I gave you and sknake's, and I thought both were working fine. What are the errors you got with the link I provided?

The code from either of those programs can be modified to countdown from 5000 of whatever you want and then execute code. What is the problem/error you are getting?

from the link, i was just finishing up the else statement after the try and catch one and it kept saying '; expected' after ''else''...

I'm not that great much of a coder myself but when I was doing this part from ksnake

"timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);"

it was saying like timer_Elapsed not found or something.

OK. Both of us provided links to completely working projects--did you not find you could unzip and build, then run either of the programs without error?

I uploaded a project that you can download and run as-is. I fail to see what issue you could possibly be having.

Ok so I downloaded your project files and debugged it and it worked out just fine, thanks. Since the names were different, I believe that the names of your project and that of mine messed up my code. Because I tried to do exactly what you did and I tried to copy yet modify some of the source to test it, you know what I mean.

But I made my own project and tried duplicating what you did but when I debugged it, it didn't do anything. The label remained ''label1'' here is the source code that I tried to do:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Timer = System.Timers.Timer;

namespace Testing_Timer
{
    public partial class Form1 : Form
    {
        private Timer timer;
        private const int requiredWaitTime = 5; // 5 second wait.
        private DateTime dtStart;

        public Form1()
        {
            InitializeComponent();
            timer = new Timer();
            timer.Interval = (1000 * 1); // 1 second wait.
            timer.AutoReset = true;
            timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
        }

        void Form1_Load(object sender, EventArgs e)
        {
            dtStart = DateTime.Now;
            timer.Enabled = true;
        }

        public delegate void SetText(string s, bool EnableButton);
        public void SetMessageText(string s, bool EnableButton)
        {
            if (label1.InvokeRequired)
            {
                label1.Invoke(new SetText(SetMessageText), s, EnableButton);
            }
            else
            {
                label1.Text = s;
                button1.Enabled = EnableButton;
            }
        }

        void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            int timeWaited = Convert.ToInt32(Math.Truncate(DateTime.Now.Subtract(dtStart).TotalSeconds));
            bool finished = timeWaited >= requiredWaitTime;
            string msg;
            if (finished)
            {
                msg = "You're ready to go!";
            }
            else
            {
                msg = string.Format("Hold on for {0:F0} more seconds!", requiredWaitTime - timeWaited);
                SetMessageText(msg, finished);
            }
        }
    }
}

thanks for the help guys =]

Upload your project. Form_Load is an event which you have to assign in the IDE. Left click on your form -- then look in your properties window and click the event tab. Select the "FormLoad" or "Load" event (I forget what its called in the IDE) then select the only item you will see in the drop down list.

The timer_elapsed event wires itself up.

sorry i have been inactive on my own thread lol.

But i searched around on google and i found an alternative task in which i kind of wanted from the start. I don't know if it's any good but I like it when the results were what i wanted

Thread.Sleep(5000);

How is that an alternative? One counts down for 5 seconds and updates a label on the way... the other one locks up an application for 5 seconds. Two different tasks my friend.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.