微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

秒倒数计时器

如何解决秒倒数计时器

| 我有一个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();

    }
    

解决方法

        为此使用计时器
   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();
    }
    ,        
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\");
}
    ,         。 用法:
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; 
当计时器对您无用时,别忘了to4。 源代码:
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,30,0);
    private DateTime _minTime = new DateTime(1,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,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();
}
    ,        您将需要使用Timer并挂钩到Tick事件中以执行所需的操作。 http://msdn.microsoft.com/zh-CN/library/system.windows.forms.timer.aspx     ,        如何使用.NET Framework中的Timer类? http://msdn.microsoft.com/zh-CN/library/0tcs6ww8.aspx     ,        嘿,请在您的项目中添加代码,这很容易,我想可以解决您的问题。
    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);

    }
    ,        您需要一个公共类供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
        }
    }
}
我已经尝试过了,一切都很好 如果您需要帮助,请随时发表评论:)     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。