简易计时窗口程序

来源:互联网 发布:南山软件测试培训 编辑:程序博客网 时间:2024/06/07 03:54

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Media;


namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        int m, s;
        string tm, ts;
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (m >=0 )
            {
                if (s == 0&&m>=1)
                {
                    s = 60;
                    m--;
                }
                if (s >= 1)
                {
                    s--;
                }
            }
            label1.Text = m.ToString() + ":" + s.ToString();
            if (label1.Text == tm+":"+ts)
            {
                
                SoundPlayer p = new SoundPlayer();
                p.SoundLocation = Application.StartupPath + "//folder.wav";
                p.Load();
                p.Play();
                //MessageBox.Show("还剩" + tm + "分" + ts + "秒!");
            }
        }
        bool d = true;
        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text ==""|| textBox2.Text =="")
            {
                MessageBox.Show("请输入时间!");
            }
            else
            {
                if (d)
                {
                    m = int.Parse(textBox1.Text);
                    if (textBox2.Text == "0" || textBox2.Text == "00")
                    {
                        s = 60;
                        m--;
                    }
                    else
                        s = int.Parse(textBox2.Text);
                    tm = textBox3.Text;
                    ts = textBox4.Text;
                    d = false;
                    timer1.Enabled = true;
                    button1.Text = "重置";
                }
                else
                {
                    d = true;
                    timer1.Enabled = false;
                    button1.Text = "开始";
                    label1.Text = textBox1.Text + ":" + textBox2.Text;
                }
            }
        }
    }
}

界面图如下:


0 0
原创粉丝点击