Seconds CountDown Timer

2020-02-10 04:08发布

问题:

I have a lblCountdown with an int value of 60. I want to make the int value of the lblCountDown decrease with seconds until it reaches 0.

This is what I have so far:

   private int counter = 60;
    private void button1_Click(object sender, EventArgs e)
    {
        int counter = 60;
        timer1 = new Timer();
        timer1.Tick += new EventHandler(timer1_Tick);
        timer1.Interval = 1000; // 1 second
        timer1.Start();
        label1.Text = counter.ToString();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        counter--;
        if (counter == 0)

            timer1.Stop();
            label1.Text = counter.ToString();

    }

回答1:

Use Timer for this

   private System.Windows.Forms.Timer timer1; 
   private int counter = 60;
   private void btnStart_Click_1(object sender, EventArgs e)
   {
        timer1 = new System.Windows.Forms.Timer();
        timer1.Tick += new EventHandler(timer1_Tick);
        timer1.Interval = 1000; // 1 second
        timer1.Start();
        lblCountDown.Text = counter.ToString();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        counter--;
        if (counter == 0)
            timer1.Stop();
        lblCountDown.Text = counter.ToString();
    }


回答2:

int segundo = 0;
DateTime dt = new DateTime();

private void timer1_Tick(object sender, EventArgs e){
    segundo++;
    label1.Text = dt.AddSeconds(segundo).ToString("HH:mm:ss");
}


回答3:

.

Usage:

CountDownTimer timer = new CountDownTimer();


//set to 30 mins
timer.SetTime(30,0);     

timer.Start();

//update label text
timer.TimeChanged += () => Label1.Text = timer.TimeLeftMsStr; 

// show messageBox on timer = 00:00.000
timer.CountDownFinished += () => MessageBox.Show("Timer finished the work!"); 

//timer step. By default is 1 second
timer.StepMs = 33; 

and don't forget to Dispose(); when timer is useless for you;


Source code:

using System;
using System.Windows.Forms;

public class CountDownTimer : IDisposable
{
    public Action TimeChanged;
    public Action CountDownFinished;

    public bool IsRunnign => timer.Enabled;

    public int StepMs
    {
        get => timer.Interval;
        set => timer.Interval = value;
    }

    private Timer timer = new Timer();

    private DateTime _maxTime = new DateTime(1, 1, 1, 0, 30, 0);
    private DateTime _minTime = new DateTime(1, 1, 1, 0, 0, 0);

    public DateTime TimeLeft { get; private set; }
    private long TimeLeftMs => TimeLeft.Ticks / TimeSpan.TicksPerMillisecond;

    public string TimeLeftStr => TimeLeft.ToString("mm:ss");

    public string TimeLeftMsStr => TimeLeft.ToString("mm:ss.fff");

    private void TimerTick(object sender, EventArgs e)
    {
        if (TimeLeftMs > timer.Interval)
        {
            TimeLeft = TimeLeft.AddMilliseconds(-timer.Interval);
            TimeChanged?.Invoke();
        }
        else
        {
            Stop();
            TimeLeft = _minTime;

            TimeChanged?.Invoke();
            CountDownFinished?.Invoke();
        }
    }

    public CountDownTimer(int min, int sec)
    {
        SetTime(min, sec);
        Init();
    }

    public CountDownTimer(DateTime dt)
    {
        SetTime(dt);
        Init();
    }

    public CountDownTimer()
    {
        Init();
    }

    private void Init()
    {
        TimeLeft = _maxTime;

        StepMs = 1000;
        timer.Tick += new EventHandler(TimerTick);
    }

    public void SetTime(DateTime dt) {
        TimeLeft = _maxTime = dt;
        TimeChanged?.Invoke();
    }

    public void SetTime(int min, int sec=0) => SetTime(new DateTime(1, 1, 1, 0, min, sec));

    public void Start() => timer.Start();

    public void Pause() => timer.Stop();

    public void Stop()
    {
        Pause();
        Reset();
    }

    public void Reset()
    {
        TimeLeft = _maxTime;
    }

    public void Restart()
    {
        Reset();
        Start();
    }

    public  void Dispose() => timer.Dispose();
}


回答4:

You'll need to use a Timer and hook into the Tick event to do what you're looking for. http://msdn.microsoft.com/en-us/library/system.windows.forms.timer.aspx



回答5:

How about using the Timer class from .NET Framework? http://msdn.microsoft.com/en-us/library/0tcs6ww8.aspx



回答6:

Hey please add code in your project,it is easy and i think will solve your problem.

    int count = 10;

    private void timer1_Tick(object sender, EventArgs e)
    {
        count--;
        if (count != 0 && count > 0)
        {
            label1.Text = count / 60 + ":" + ((count % 60) >= 10 ? (count % 60).ToString() : "0" + (count % 60));
        }
        else
        {
            label1.Text = "game over";

        }

    }

    private void Form1_Load(object sender, EventArgs e)
    {
        timer1 = new System.Windows.Forms.Timer();
        timer1.Interval = 1;

        timer1.Tick += new EventHandler(timer1_Tick);

    }


回答7:

You need a public class for Form1 to initialize.

See this code:

namespace TimerApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private int counter = 60;
        private void button1_Click(object sender, EventArgs e)
        {
            //Insert your code from before
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //Again insert your code
        }
    }
}

I've tried this and it all worked fine

If you need anymore help feel free to comment :)



标签: c# string timer