C#倒計時


示意圖:六個lable,三個文本框,一個but即可  timer控件或自定義一個

 

以下是代碼實現的部分

public int ConvertInt32(string name)
        {
            if (name != "" && name != null)
            {
                return Convert.ToInt32(name);
            }
            else
            {
                return 0;
            }
        }
        int Hose, Minut, Second = 0;
        //public void ComdTimer()
        //{
        //    if (button1.Text == "設置")//判斷按鈕的文本是否為設置
        //    {
        //        button1.Text = "停止";//設置按鈕的文本為停止
        //        timer2.Start();//啟動timer2計時器
        //    }
        //    else if (button1.Text == "停止")//判斷按鈕的文本是否為停止
        //    {
        //        button1.Text = "設置";//設置按鈕的文本為設置
        //        timer2.Stop();//停止timer2計時器
        //        label3.Text = "倒計時已取消";
        //    }
        //}
        

        private void button1_Click(object sender, EventArgs e)
        {
            Hose = ConvertInt32(textBox1.Text.Trim());
            if (ConvertInt32(textBox2.Text.Trim()) > 60)
            {
                MessageBox.Show("不能大於60分鍾");
                return;
            }
            else
            {
                Minut = ConvertInt32(textBox2.Text.Trim());
            }
            if (ConvertInt32(textBox3.Text.Trim()) >= 60)
            {
                MessageBox.Show("不能大於60秒");
                return;
            }
            else
            {
                Second = ConvertInt32(textBox3.Text.Trim());
            }
            this.timer1.Start();
            this.timer1.Enabled = true;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (Second != 0)
            {
                Second--;
                //給Viz發東西
            }
            if (Second == 0)
            {
                if (Minut != 0)
                {
                    Second += 60;
                    Minut--;
                }
            }
            if (Minut == 0)
            {
                if (Hose != 0)
                {
                    Minut += 60;
                    Hose--;
                }
            }
            label3.Text = Hose.ToString();
            label2.Text = Minut.ToString();
            label1.Text = Second.ToString();
        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM