0

I'm working on a typing test program for fun and practice coding, and I figured the easiest thing to do first would be to get the counter out of the way. This doesn't at all seem to be 60 seconds like I want it to be, I think it's too short a time, however the counter does go down, so that is a plus. I've never actually worked with the timer in windows forms at all, so I've got a lot to explore.

namespace Countdown
{
    public partial class Form1 : Form
    {   


    int s = 60;
    public Form1()
    {

        InitializeComponent();
        label1.Text = s.ToString();
    }

    private void label1_Click(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {

        timer1.Start();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        s -= 1;
        if(s == 0)
        {
            timer1.Stop();
            button1.Enabled = false;
            textBox1.Enabled = false;
        }

        string ss = Convert.ToString(s);
        label1.Text = ss;
    }
} 
Patrick
  • 5
  • 2
  • you need to set `Timer.Interval` to value in milliseconds (60,000 in your case). `Tick` event will fire when those milliseconds have passed. – Nino Mar 25 '17 at 06:54
  • Assuming you set your timer interval to 1000 ms, as the code implies, you still cannot expect that to be accurate. See marked duplicates for extended discussion of why. Short version is that the timer doesn't execute the handler _exactly_ on the interval; mainstream OS's just aren't that precise (including Windows). You need to track the time using some other mechanism and use the timer only to obtain periodic updates of the current time. – Peter Duniho Mar 25 '17 at 07:23

0 Answers0