何か勉強するとき時間を記録するといいのかもと思ったので。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
using System; using System.Windows.Forms; using System.Xml.Serialization; using System.IO; namespace StudyTimer { public partial class Form1 : Form { private DateTime Times; public Form1() { InitializeComponent(); this.label1.Text = "00:00:00"; if (this.textBox1.Text == "") this.textBox1.Text = "0"; if (this.textBox2.Text == "") this.textBox2.Text = "0"; timer1.Tick += new EventHandler((object sender, EventArgs e) => { this.label1.Text = (DateTime.Now - Times).ToString(@"hh\:mm\:ss"); }); XmlSerializer xs = new XmlSerializer(typeof(int)); using (StreamReader sr = new StreamReader(Application.StartupPath + @"\dat", new System.Text.UTF8Encoding(false))) { try { this.textBox1.Text = xs.Deserialize(sr).ToString(); this.textBox2.Text = (int.Parse(this.textBox1.Text) / 60).ToString(); } catch { MessageBox.Show("読込失敗"); return; } finally { sr.Close(); } } } private void button1_Click(object sender, EventArgs e) { timer1.Start(); Times = DateTime.Now; } private void button2_Click(object sender, EventArgs e) { if (timer1.Enabled) { timer1.Stop(); } } private void button3_Click(object sender, EventArgs e) { if (timer1.Enabled) { timer1.Stop(); } this.label1.Text = "00:00:00"; } private void Serialize() { XmlSerializer xs = new XmlSerializer(typeof(int)); using (StreamWriter sw = new StreamWriter(Application.StartupPath + @"\dat", false, new System.Text.UTF8Encoding(false))) { try { xs.Serialize(sw,int.Parse(this.textBox1.Text)); } catch { MessageBox.Show("保存失敗"); return; } finally { sw.Close(); } } } private void button4_Click(object sender, EventArgs e) { DateTime t = DateTime.Parse(label1.Text); int m = t.Minute + int.Parse(this.textBox1.Text); this.textBox1.Text = m.ToString(); this.textBox2.Text = (m / 60).ToString(); Serialize(); } } } |