小闹铃开始记忆啦
【技术要点】
StreamReader类,实现对文本文件的读操作。
ReadLine()从基础字符窜中读取一行。
StreamWriter类,实现对文本文件的写操作。
WriteLine()从写入重载参数指定的某些数据,后跟行结束符。
【实现步骤】
——代码
在Form1_Load中加入将文本文件内容读入字典集合代码:
StreamReader ts = File.OpenText("C:\\Users\\AZHNBO\\Documents\\time.txt");//打开规则文本文件
stringkey = ts.ReadLine();
stringvalue = ts.ReadLine();
//判断是否有记录
while(key != null){
rule.Add(key, value);//依次将规则读入字典集合
key = ts.ReadLine();
value= ts.ReadLine(); }
ts.Close();
在Form1_FormClosing中加入将字典集合写入文本文件代码:
StreamWriter ts = File.CreateText("C:\\Users\\AZHNBO\\Documents\\time.txt");//创建规则文本文件
for(int i = 0; i < rule.Count; i++){//依次将字典集合写入规则文件
ts.WriteLine(rule.Keys[i]);
ts.WriteLine(rule.Values[i]); }
ts.Close();
——改进
实现多任务提醒
【主要代码】
public partial class Form1 : Form
{
public static bool flag = false;//表示托盘图标是否闪烁
public SortedList<string, object> rule = new SortedList<string, object>();//声明存储规则的字典集合
public string current_time;//存储当前时间值
public Form1()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
//比较当前与目标时间
if (rule.ContainsKey(DateTime.Now.ToString()))//确定是否包含特定键值
{
timer1.Enabled = false;//暂停timer1
current_time = DateTime.Now.ToString ();//保存当前时间
timer2.Enabled = true;//启动timer2
}
else
{
label1.Text = DateTime.Now.ToString();//在label1显示当前时间
}
}
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Text= DateTime.Now.ToString ();//用符号区别输入
textBox2.Text= "_";//防止空字符
timer1.Enabled = true;//启动timer1
StreamReader ts = File.OpenText("C:\\Users\\AZHNBO\\Documents\\time.txt");//打开规则文本文件
string key = ts.ReadLine();
string value = ts.ReadLine();
//判断是否有记录
while (key != null)
{
rule.Add(key, value);//依次将规则读入字典集合
key = ts.ReadLine();
value= ts.ReadLine();
}
ts.Close();
button1.Text = "规则" + rule.Count.ToString();
}
private void timer2_Tick(object sender, EventArgs e)
{
//比较托盘图标标志
if (flag == false)
{
notifyIcon1.Visible =true;//托盘图标显示
notifyIcon1.ShowBalloonTip(1000, "时间到",rule[current_time].ToString (), ToolTipIcon.Info);//任务栏气泡提示
flag = true;
}
else
{
notifyIcon1.Visible =false;//托盘图标隐藏
flag = false;
}
}
private void notifyIcon1_BalloonTipClicked(object sender, EventArgs e)
{
rule.Remove(current_time);
button1.Text = "规则" + rule.Count.ToString();
timer1.Enabled = true;//启动timer1
timer2.Enabled = false;//暂停timer2
}
private void button1_Click(object sender, EventArgs e)
{
rule.Add(textBox1.Text.ToString(), textBox2.Text.ToString());//获取规则加入字典集合
button1.Text = "规则" + rule.Count.ToString();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
StreamWriter ts = File .CreateText("C:\\Users\\AZHNBO\\Documents\\time.txt");//创建规则文本文件
for (int i = 0; i < rule.Count; i++)//依次将字典集合写入规则文件
{
ts.WriteLine(rule.Keys[i]);
ts.WriteLine(rule.Values[i]);
}
ts.Close();
}
}