本文介绍了秒倒计时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这是我目前所拥有的:

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

    }

推荐答案

为此使用 Timer

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

这篇关于秒倒计时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-29 09:30