【问题标题】:Seconds CountDown Timer秒倒数计时器
【发布时间】:2011-05-31 18:01:35
【问题描述】:

我有一个 int 值为 60 的 lblCountdown。我想让 lblCountDown 的 int 值随着秒数减少,直到它达到 0。

这是我目前所拥有的:

   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();

    }

【问题讨论】:

    标签: c# string timer


    【解决方案1】:

    为此使用计时器

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

    【讨论】:

    • 这个计时器不够准确。特别是在间隔会很低的情况下。但即使有 1 秒的间隔也会很糟糕。
    【解决方案2】:

    (2020 年 6 月 6 日更新,因为时间计算有问题)

    用法:

    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 = 77; // for nice milliseconds time switch
    

    当计时器对你没用时,别忘了Dispose();

    源代码:

    using System;
    using System.Diagnostics;
    using System.Windows.Forms;
    
    public class CountDownTimer : IDisposable
    {
        public Stopwatch _stpWatch = new Stopwatch();
    
        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 TimeSpan _max = TimeSpan.FromMilliseconds(30000);
    
        public TimeSpan TimeLeft => (_max.TotalMilliseconds - _stpWatch.ElapsedMilliseconds) > 0 ? TimeSpan.FromMilliseconds(_max.TotalMilliseconds - _stpWatch.ElapsedMilliseconds) : TimeSpan.FromMilliseconds(0);
        
        private bool _mustStop => (_max.TotalMilliseconds - _stpWatch.ElapsedMilliseconds) < 0;
    
        public string TimeLeftStr => TimeLeft.ToString(@"\mm\:ss");
    
        public string TimeLeftMsStr => TimeLeft.ToString(@"mm\:ss\.fff");
    
        private void TimerTick(object sender, EventArgs e)
        {
            TimeChanged?.Invoke();
    
            if (_mustStop)
            {
                CountDownFinished?.Invoke();
                _stpWatch.Stop();
                timer.Enabled = false;
            }
        }
    
        public CountDownTimer(int min, int sec)
        {
            SetTime(min, sec);
            Init();
        }
    
        public CountDownTimer(TimeSpan ts)
        {
            SetTime(ts);
            Init();
        }
    
        public CountDownTimer()
        {
            Init();
        }
    
        private void Init()
        {
            StepMs = 1000;
            timer.Tick += new EventHandler(TimerTick);
        }
    
        public void SetTime(TimeSpan ts)
        {
            _max = ts;
            TimeChanged?.Invoke();
        }
    
        public void SetTime(int min, int sec = 0) => SetTime(TimeSpan.FromSeconds(min * 60 + sec));
    
        public void Start() {
            timer.Start();
            _stpWatch.Start();
        }
    
        public void Pause()
        {
            timer.Stop();
            _stpWatch.Stop();
        }
    
        public void Stop()
        {
            Reset();
            Pause();
        }
    
        public void Reset()
        {
            _stpWatch.Reset();
        }
    
        public void Restart()
        {
            _stpWatch.Reset();
            timer.Start();
        }
    
        public void Dispose() => timer.Dispose();
    }
    

    【讨论】:

      【解决方案3】:
      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");
      }
      

      【讨论】:

        【解决方案4】:

        这是我的解决方案,它可能对某人有所帮助。你需要一个标签和一个计时器。 我在表单加载时写了它,因为我正在处理一个个人项目,但你可以通过将它放在一个按钮中来添加它的功能。

        private void Form1_Load(object sender, EventArgs e){
                timer1.Interval = 1000;
                timer1.Start();
        }
        
        private void timer1_Tick(object sender, EventArgs e){
                if (int.Parse(label_time_left.Text) != 0){
                    int futureText = int.Parse(label_time_left.Text) - 1;
                    label_time_left.Text = futureText.ToString();
                } else {
                    // actions when the timer ends 
                }
        }
        

        正如您所见,它的工作方式非常简单 :) 只是解析

        【讨论】:

          【解决方案5】:

          嘿,请在您的项目中添加代码,这很简单,我认为可以解决您的问题。

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

          【讨论】:

            【解决方案6】:

            您需要一个公共类来初始化 Form1。

            查看此代码:

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

            我试过了,一切正常

            如果您需要更多帮助,请随时发表评论:)

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2016-02-05
              • 1970-01-01
              • 1970-01-01
              • 2014-12-14
              • 1970-01-01
              相关资源
              最近更新 更多