winform闹钟

来源:互联网 发布:卡拉ok点歌软件 编辑:程序博客网 时间:2024/04/28 00:16

实现了一个简单的闹钟,代码的很多地方都需要优化,只是抛砖引玉,希望童鞋们把自己的设计拿出来

功能:

1.可以添加多个闹钟,为闹钟设定时间和铃音

2.可以更改,删除闹钟

3.闹钟铃音可以随意控制停止和开始

操作方法

1.添加闹钟:在小时和分钟文本框中输入相应的时间,点击“添加”按钮,为闹钟选择铃音,添加完成

2.更改闹钟:选中相应的闹钟,闹钟的时间会显示小时和分钟文本框中,更改之后点击“更改闹钟”,更改完成

3.删除闹总,选中相应闹钟,点击“删除闹钟”按钮,删除闹钟

闹钟界面截图


程序代码:

1.建立闹钟类Alarm_clock

//闹钟类

class Alarm_clock    {        //闹钟是否启用        public bool IsEnabel { get; set; }        //设定的闹钟时间        public string TheTime { get; set; }        //闹钟铃音的URL        public string TimeUrl { get; set; }    }



    Form1.cs

public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        //闹钟集合(用户可以设定多个闹钟),除了第一次窗口加载时从文件中加载闹钟信息之外,其余对闹钟的所有操作,包括添加,删除,更改都对此集合进行操作,只有在窗口关闭时才将更改保存到文件中        List<Alarm_clock> list = new List<Alarm_clock>();        //存储用户选择的闹钟的时间        string selecttime = string.Empty;                private void Form1_Load(object sender, EventArgs e)        {            //窗体加载时从文件中加载所有的闹钟信息到List集合中            LoadConfig();            //将所有闹钟信息显示到窗体上的ListBox上            ShowInfo();        }        //从配置文件中加载闹钟信息        private void LoadConfig()        {            using (Stream stream = File.Open("config.txt", FileMode.Open, FileAccess.Read))            {                using (StreamReader sr = new StreamReader(stream))                {                    string line = string.Empty;                    while (!string.IsNullOrEmpty(line = sr.ReadLine()))                    {                        string[] strs = line.Split('|');                        list.Add(new Alarm_clock()                        {                            TheTime = strs[0],                            TimeUrl = strs[1],                            IsEnabel = false                        });                    }                }            }        }        //更新窗体上的ListBox上显示的闹钟信息        private void ShowInfo()        {            //删除ListBox的所有项            for (int i = this.listBox1.Items.Count - 1; i >= 0; i--)            {                this.listBox1.Items.Remove(listBox1.Items[i]);            }           //将List集合中的所有项都添加到ListBox中            foreach (Alarm_clock item in list)            {                this.listBox1.Items.Add(item.TheTime + "|........铃音:" + Path.GetFileName(item.TimeUrl));            }        }        //添加闹钟        private void btnAdd_Click(object sender, EventArgs e)        {            string hours = this.txtHours.Text;            string minutes = this.txtMinutes.Text;            #region 验证时间格式            //验证小时格式            if (!Regex.IsMatch(hours, @"^[0-9]{1,2}$"))            {                MessageBox.Show("请输入正确的小时格式");                return;            }            if (Convert.ToInt32(hours) > 23)            {                MessageBox.Show("请输入有效的时间范围");                return;            }            //验证分钟            if (!Regex.IsMatch(minutes, @"^[0-9]{1,2}$"))            {                MessageBox.Show("请输入正确的分钟格式");                return;            }            if (Convert.ToInt32(minutes) > 60)            {                MessageBox.Show("请输入有效的时间范围");                return;            }            #endregion                        //设置闹钟铃音            OpenFileDialog of = new OpenFileDialog();            of.Title = "选择背景音乐";            of.AddExtension = true;            of.CheckFileExists = true;            of.CheckPathExists = true;            of.Filter = "Mp3文件(*.mp3)|*.mp3|VCD文件(*.dat)|*.dat|Audio文件(*.avi)|*.avi";            of.DefaultExt = "*.mp3";            if (of.ShowDialog() == DialogResult.OK)            {                //等窗口关闭时再将List中的内容写到文件中,这样避免了每次添加新的闹钟,数据重新加载                list.Add(new Alarm_clock()                {                    TheTime = hours + ":" + minutes,                    TimeUrl = of.FileName,                    IsEnabel = false                });                ShowInfo();            }        }        //更改闹钟        private void btnUpdate_Click(object sender, EventArgs e)        {            if (MessageBox.Show("确定要更改吗?", "闹钟更改", MessageBoxButtons.YesNo) == DialogResult.Yes)            {                foreach (Alarm_clock item in list)                {                    if (item.TheTime == selecttime)                    {                        item.TheTime = this.txtHours.Text + ":" + this.txtMinutes.Text;                        ShowInfo();                    }                }            }        }        private void timer1_Tick(object sender, EventArgs e)        {            DateTime thetime = DateTime.Now;            string strtime = thetime.Hour.ToString() + ":" + thetime.Minute.ToString();            foreach (Alarm_clock item in list)            {                if (item.TheTime == strtime)                {                    if (item.IsEnabel == false)                    {                        Thread mythread = new Thread(AutoMp3);                        mythread.Start(item.TimeUrl);                        //猜猜这里设定item.IsEnabel = true的原因,试试去掉这行代码的效果                        item.IsEnabel = true;                    }                }            }        }        private void AutoMp3(Object url)        {            //只有在为WindowsMediaPlay控件的URL属性赋值之后铃音才会播放            this.axWindowsMediaPlayer1.URL = url.ToString();        }        //用户关闭窗口时将List中最新的闹钟信息返写到文本文件中        private void Form1_FormClosed(object sender, FormClosedEventArgs e)        {            File.Delete("config.txt");            using (Stream stream = File.Open("config.txt", FileMode.Append, FileAccess.Write))            {                using (StreamWriter sw = new StreamWriter(stream))                {                    foreach (Alarm_clock item in list)                    {                        sw.WriteLine(item.TheTime + "|" + item.TimeUrl);                    }                }            }        }        //用户选择ListBox中的某个闹钟时,将此闹钟的时间显示到上面的文本框中        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)        {            if (this.listBox1.SelectedItem != null)            {                string[] infos = this.listBox1.SelectedItem.ToString().Split('|');                string[] thetime = infos[0].Split(':');                this.txtHours.Text = thetime[0];                this.txtMinutes.Text = thetime[1];                selecttime = txtHours.Text + ":" + txtMinutes.Text;            }                   }        //删除闹钟        private void btnDelete_Click(object sender, EventArgs e)        {            if (this.listBox1.SelectedItem != null)            {                if (MessageBox.Show("确定删除吗?", "闹钟删除", MessageBoxButtons.YesNo) == DialogResult.Yes)                {                    for (int i = 0; i < list.Count;i++ )                    {                        Alarm_clock arm = list[i] as Alarm_clock;                        if (arm.TheTime == selecttime)                        {                            list.RemoveAt(i);                            ShowInfo();                        }                    }                }            }        }    }