private void btnAdd_Click(object sender, EventArgs e) { TimeAlert item = new TimeAlert(); item.Message = this.txtMessage.Text; item.AlertTime = this.dtTime.Value; if (radioButton1.Checked) { item.SplitType = TimeAlert.SplitTypeEnum.OnlyOne; } if (radioButton2.Checked) { item.SplitType = TimeAlert.SplitTypeEnum.Day; } if (radioButton3.Checked) { item.SplitType = TimeAlert.SplitTypeEnum.Month; } if (radioButton4.Checked) { item.SplitType = TimeAlert.SplitTypeEnum.Year; } bool isExist = false; foreach (TimeAlert r in Constants.AlertList) { if (r.SplitType == item.SplitType && r.AlertTime == item.AlertTime) { if (item.Message == "") { //如果已经有提醒了,新添提醒内容为空,则不处理 return; } if (r.Message == "") { r.Message = item.Message; } else { r.Message += "; " + item.Message; } isExist = true; break; } } if (!isExist) { Constants.AlertList.Add(item); } this.txtMessage.Text = ""; SaveSet(); ResetList(); }
private void SaveSet() { string str = ""; str += (Constants.IsHourAlert ? "整点报时" : "不整点报时") + ";"; str += (Constants.IsHalfHourAlert ? "半点报时" : "不半点报时") + ";"; str += (Constants.IsSound ? "声音提示" : "不声音提示") + ";"; str += "\r\n"; foreach (TimeAlert r in Constants.AlertList) { str += TimeAlert.GetSplitTypeName(r.SplitType) + "|"; switch (r.SplitType) { case TimeAlert.SplitTypeEnum.OnlyOne: str += r.AlertTime.ToString("yyyy-MM-dd HH:mm"); break; case TimeAlert.SplitTypeEnum.Day: str += r.AlertTime.ToString("HH:mm"); break; case TimeAlert.SplitTypeEnum.Month: str += r.AlertTime.ToString("dd HH:mm"); break; case TimeAlert.SplitTypeEnum.Year: str += r.AlertTime.ToString("MM-dd HH:mm"); break; default: break; } str += "|" + r.Message + "\r\n"; } if (oldString == str) { return; } oldString = str; File.Delete(FileSetPath); File.AppendAllText(FileSetPath, str); }
private void ResetList() { this.listViewAlert.BeginUpdate(); this.listViewAlert.Items.Clear(); foreach (TimeAlert r in Constants.AlertList) { string strTime = ""; switch (r.SplitType) { case TimeAlert.SplitTypeEnum.OnlyOne: strTime = r.AlertTime.ToString("yyyy年MM月dd日 HH时mm分"); break; case TimeAlert.SplitTypeEnum.Day: strTime = r.AlertTime.ToString("HH时mm分"); break; case TimeAlert.SplitTypeEnum.Month: strTime = r.AlertTime.ToString("dd日 HH时mm分"); break; case TimeAlert.SplitTypeEnum.Year: strTime = r.AlertTime.ToString("MM月dd日 HH时mm分"); break; default: break; } string[] str = { TimeAlert.GetSplitTypeName(r.SplitType), strTime, r.Message }; ListViewItem item = new ListViewItem(str); if (r.SplitType == TimeAlert.SplitTypeEnum.OnlyOne && r.AlertTime < DateTime.Now) { item.ForeColor = Color.Red; } this.listViewAlert.Items.Add(item); } this.listViewAlert.EndUpdate(); }
private static bool IsOld(TimeAlert item) { return(item.SplitType == TimeAlert.SplitTypeEnum.OnlyOne && item.AlertTime < DateTime.Now); }
private void Form1_Load(object sender, EventArgs e) { FileSetPath = Application.StartupPath + "\\set.txt"; if (File.Exists(FileSetPath)) { //加载设置 oldString = File.ReadAllText(FileSetPath); string[] strs = oldString.Split('\r'); this.chkHour.Checked = strs[0].IndexOf("不整点报时") == -1; this.chkHalfHour.Checked = strs[0].IndexOf("不半点报时") == -1; this.chkSound.Checked = strs[0].IndexOf("不声音提示") == -1; Constants.IsHourAlert = this.chkHour.Checked; Constants.IsHalfHourAlert = this.chkHalfHour.Checked; Constants.IsSound = this.chkSound.Checked; Constants.AlertList = new List <TimeAlert>(); for (int i = 1; i < strs.Length; i++) { if (strs[i].Trim() == "") { continue; } string[] strAlert = strs[i].Trim().Split('|'); if (strAlert.Length == 3) { TimeAlert item = new TimeAlert(); item.Message = strAlert[2]; item.SplitType = TimeAlert.GetSplitType(strAlert[0]); switch (item.SplitType) { case TimeAlert.SplitTypeEnum.OnlyOne: item.AlertTime = DateTime.Parse(strAlert[1]); break; case TimeAlert.SplitTypeEnum.Day: item.AlertTime = DateTime.Parse("2000-01-01 " + strAlert[1]); break; case TimeAlert.SplitTypeEnum.Month: item.AlertTime = DateTime.Parse("2000-01-" + strAlert[1]); break; case TimeAlert.SplitTypeEnum.Year: item.AlertTime = DateTime.Parse("2000-" + strAlert[1]); break; default: break; } Constants.AlertList.Add(item); } } ResetList(); } ImageList l = new ImageList(); l.ImageSize = new Size(1, 23); this.listViewAlert.SmallImageList = l; SelectTime(TimeAlert.SplitTypeEnum.OnlyOne); //Constants.AlertList.Add(new TimeAlert("测试提醒!", TimeAlert.SplitTypeEnum.OnlyOne, DateTime.Now.AddSeconds(10))); this.timer.Start(); isInitFinish = true; }