砂時計ツール
俗に言うキッチンタイマー。個人的に使うもので、エラー処理はほとんど行っていない。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace MyClock { public partial class Form1 : Form { const string btn_clock_start = "測定開始"; const string btn_clock_stop = "停止"; DateTime start_Time; // 計測を開始した時間 int limit_Time; // 計測する時間の長さ(分) public Form1() { InitializeComponent(); button1.Text = btn_clock_start; } private void button1_Click(object sender, EventArgs e) { if (button1.Text == btn_clock_start) { clock_Start(); } else { timer1.Enabled = false; clock_Stop(); } } // 計測開始 private void clock_Start() { limit_Time = int.Parse(comboBox1.Text); if (limit_Time <= 0) { return; } button1.Text = btn_clock_stop; timer1.Enabled = true; start_Time = DateTime.Now; } // 計測終了 private void clock_Stop() { button1.Text = btn_clock_start; timer1.Enabled = false; progressBar1.Value = 0; } private void timer1_Tick(object sender, EventArgs e) { TimeSpan ts = DateTime.Now.Subtract(start_Time); int value = (int)(100 * ts.TotalMinutes / limit_Time); progressBar1.Value = value; if (ts.Minutes >= limit_Time) { clock_Stop(); MessageBox.Show("終了しました。"); } } } }