3. 電腦螢幕倒數計時器

namespace Q3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            timer1.Interval = 1000;
        }

        int n = 0;
        private void timer1_Tick(object sender, EventArgs e)
        {
            n--;
            label3.Text = (n / 60).ToString();
            label2.Text = (n % 60).ToString();
            if (n == 0)
            {
                timer1.Stop();
                MessageBox.Show("時間到", null, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                button1.Text = "開始";
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (timer1.Enabled)
            {
                timer1.Stop();
                button1.Text = "開始";
            }
            else
            {
                timer1.Start();
                button1.Text = "停止";
                n = int.Parse(label3.Text) * 60 + int.Parse(label2.Text);
            }
        }
    }
}

Last updated