winform 怎么用Timer空间做一个25分钟的倒计时

用Timer空间做一个25分钟的倒计时显示在一个标签里。要求显示只有25:00分钟和秒这样的。往下减。下一秒变成24:59。就像这样的。怎么做?怎么定义。只要分和秒

1)窗体

图中窗体的名称为Form2。换成其他名称,如Form1也可以

2)后台代码

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{

    public partial class Form2 : Form
    {
        private int Seconds; 
        
        public Form2()
        {
            InitializeComponent();
            // 定时器
            timer1.Interval = 1000;
            timer1.Enabled = false;
            // 定时间隔:25分钟
            Seconds = 25 * 60;
            ShowTime();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // 启动倒计时
            timer1.Start();
        }

        private void ShowTime()
        {
            // 显示剩余时间
            label1.Text = string.Format("{0:d2}:{1:d2}", 
                    Seconds / 60, 
                    Seconds % 60);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Seconds--;
            if (Seconds < 0)
            {
                // 停止倒计时
                timer1.Stop();
                return;
            }
            ShowTime();
        }
    }
}

温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答