using System; using System.Drawing; using System.Collections; using System.Windows.Forms; using System.Data; namespace Timer { /// /// Form1 の概要の説明です。 /// public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.TextBox txtTime; private System.Windows.Forms.Button btnStop; private System.Windows.Forms.Button btnStart; private System.Windows.Forms.Timer Timer1; private System.Windows.Forms.MenuItem menuItem1; private System.Windows.Forms.MenuItem menuItem2; private System.Windows.Forms.MenuItem menuItem3; private System.Windows.Forms.MenuItem menuItem4; private System.Windows.Forms.MainMenu mainMenu1;
public Form1() { // // Windows フォーム デザイナ サポートに必要です。 // InitializeComponent(); // // TODO: InitializeComponent 呼び出しの後に、コンストラクタ コードを追加してください。 // } /// /// 使用されているリソースに後処理を実行します。 ///
protected override void Dispose( bool disposing ) { base.Dispose( disposing ); }
Windows フォーム デザイナで生成されたコード
/// /// アプリケーションのメイン エントリ ポイントです。 /// static void Main() { Application.Run(new Form1()); } // 終了時間保存用変数 private DateTime endTime; // タイマー時間 private int icount = 180;
// スタートボタンでタイマーを開始する処理 private void btnStart_Click(object sender, System.EventArgs e) { endTime = DateTime.Now.AddSeconds(icount); this.Timer1.Enabled = true; } // 1秒ごとに残り時間を表示し、残り0秒でメッセージを出す処理 private void Timer1_Tick(object sender, System.EventArgs e) {
TimeSpan time; time = endTime.Subtract(DateTime.Now); this.txtTime.Text = time.ToString(); if( time.TotalSeconds <= 0.0) { this.Timer1.Enabled = false; MessageBox.Show("時間だよ!"); } } // STOPボタンで一時停止もしくはリセットをする処理 private void btnStop_Click(object sender, System.EventArgs e) { this.ResetTimer("00:03:00", 180);
if( Timer1.Enabled == true ) { this.Timer1.Enabled = false; TimeSpan time; time = endTime.Subtract(DateTime.Now); icount = (int)(time.TotalSeconds); } }
// タイマーをリセットするための共通関数 private void ResetTimer( string time, int count ) {
if( Timer1.Enabled == false ) { this.txtTime.Text = time; icount = count; } } // 1分タイマー設定 private void menuItem2_Click(object sender, System.EventArgs e) { this.ResetTimer("00:01:00", 60); }
// 3分タイマー設定 private void menuItem3_Click(object sender, System.EventArgs e) { this.ResetTimer("00:03:00", 180); } // 5分タイマー設定 private void menuItem4_Click(object sender, System.EventArgs e) { this.ResetTimer("00:05:00", 300); } } }
|