/ Published in: C#
Basic egg timer, you set a time in 00:00 format and click the action button (Start, Stop, Reset) based on where the timer state (Stopped, Started, Done) respectively
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
using System; using System.Windows.Forms; namespace EggTimer { static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); } } public class Form1 : Form { public Form1() { InitializeComponent(); } private void timer_Tick(object sender, EventArgs e) { if (ts.TotalSeconds <= 0) { finishedTimer(); } else { timeBox.Text = ts.ToString("mm\\:ss"); } } private void actionButton_Click(object sender, EventArgs e) { if (actionButton.Text == "Start") { startTimer(); } else if (actionButton.Text == "Reset") { reinitializeTimer(); } else { stopTimer(); } } private void startTimer() { string[] minSec = timeBox.Text.Split(':'); timer.Start(); actionButton.Text = "Stop"; } private void stopTimer() { timer.Stop(); actionButton.Text = "Start"; } private void finishedTimer() { timer.Stop(); timeBox.Text = "-00:00-"; actionButton.Text = "Reset"; } private void reinitializeTimer() { timeBox.Text = "03:00"; actionButton.Text = "Start"; } private void timeBox_TextChanged(object sender, EventArgs e) { } private void timeBox_KeyPress(object sender, KeyPressEventArgs e) { if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != ':') e.Handled = true; } /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.SuspendLayout(); // // timeBox // this.timeBox.Font = new System.Drawing.Font("Microsoft Sans Serif", 48F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.timeBox.Name = "timeBox"; this.timeBox.TabIndex = 1; this.timeBox.Text = "03:00"; this.timeBox.TextAlign = System.Windows.Forms.HorizontalAlignment.Right; // // actionButton // this.actionButton.Font = new System.Drawing.Font("Microsoft Sans Serif", 48F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.actionButton.Name = "actionButton"; this.actionButton.TabIndex = 0; this.actionButton.Text = "Start"; this.actionButton.UseVisualStyleBackColor = true; // // timer // this.timer.Interval = 1000; // // Form1 // this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.Controls.Add(this.actionButton); this.Controls.Add(this.timeBox); this.Name = "Form1"; this.Text = "Egg Timer"; this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.TextBox timeBox; private System.Windows.Forms.Button actionButton; private System.Windows.Forms.Timer timer; } }