Egg Timer - c# winforms


/ Published in: C#
Save to your folder(s)

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


Copy this code and paste it in your HTML
  1. using System;
  2. using System.Windows.Forms;
  3.  
  4. namespace EggTimer
  5. {
  6. static class Program
  7. {
  8. /// <summary>
  9. /// The main entry point for the application.
  10. /// </summary>
  11. [STAThread]
  12. static void Main()
  13. {
  14. Application.EnableVisualStyles();
  15. Application.SetCompatibleTextRenderingDefault(false);
  16. Application.Run(new Form1());
  17. }
  18. }
  19.  
  20. public class Form1 : Form
  21. {
  22. private TimeSpan ts = new TimeSpan();
  23.  
  24. public Form1()
  25. {
  26. InitializeComponent();
  27. }
  28.  
  29. private void timer_Tick(object sender, EventArgs e)
  30. {
  31. ts = ts.Subtract(new TimeSpan(0, 0, 1));
  32. if (ts.TotalSeconds <= 0)
  33. {
  34. finishedTimer();
  35. }
  36. else
  37. {
  38. timeBox.Text = ts.ToString("mm\\:ss");
  39. }
  40. }
  41.  
  42. private void actionButton_Click(object sender, EventArgs e)
  43. {
  44. if (actionButton.Text == "Start")
  45. {
  46. startTimer();
  47. }
  48. else if (actionButton.Text == "Reset")
  49. {
  50. reinitializeTimer();
  51. }
  52. else
  53. {
  54. stopTimer();
  55. }
  56. }
  57.  
  58. private void startTimer()
  59. {
  60. string[] minSec = timeBox.Text.Split(':');
  61. ts = new TimeSpan(0, int.Parse(minSec[0]), int.Parse(minSec[1]));
  62. timer.Start();
  63. actionButton.Text = "Stop";
  64. }
  65.  
  66. private void stopTimer()
  67. {
  68. timer.Stop();
  69. actionButton.Text = "Start";
  70. }
  71.  
  72. private void finishedTimer()
  73. {
  74. timer.Stop();
  75. timeBox.Text = "-00:00-";
  76. actionButton.Text = "Reset";
  77. }
  78.  
  79. private void reinitializeTimer()
  80. {
  81. timeBox.Text = "03:00";
  82. actionButton.Text = "Start";
  83. }
  84.  
  85. private void timeBox_TextChanged(object sender, EventArgs e)
  86. {
  87.  
  88. }
  89.  
  90. private void timeBox_KeyPress(object sender, KeyPressEventArgs e)
  91. {
  92. if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != ':')
  93. e.Handled = true;
  94. }
  95.  
  96. /// <summary>
  97. /// Required designer variable.
  98. /// </summary>
  99. private System.ComponentModel.IContainer components = null;
  100.  
  101. /// <summary>
  102. /// Clean up any resources being used.
  103. /// </summary>
  104. /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  105. protected override void Dispose(bool disposing)
  106. {
  107. if (disposing && (components != null))
  108. {
  109. components.Dispose();
  110. }
  111. base.Dispose(disposing);
  112. }
  113.  
  114. #region Windows Form Designer generated code
  115.  
  116. /// <summary>
  117. /// Required method for Designer support - do not modify
  118. /// the contents of this method with the code editor.
  119. /// </summary>
  120. private void InitializeComponent()
  121. {
  122. this.components = new System.ComponentModel.Container();
  123. System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
  124. this.timeBox = new System.Windows.Forms.TextBox();
  125. this.actionButton = new System.Windows.Forms.Button();
  126. this.timer = new System.Windows.Forms.Timer(this.components);
  127. this.SuspendLayout();
  128. //
  129. // timeBox
  130. //
  131. this.timeBox.Font = new System.Drawing.Font("Microsoft Sans Serif", 48F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  132. this.timeBox.Location = new System.Drawing.Point(12, 12);
  133. this.timeBox.Name = "timeBox";
  134. this.timeBox.Size = new System.Drawing.Size(260, 80);
  135. this.timeBox.TabIndex = 1;
  136. this.timeBox.Text = "03:00";
  137. this.timeBox.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
  138. this.timeBox.TextChanged += new System.EventHandler(this.timeBox_TextChanged);
  139. this.timeBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.timeBox_KeyPress);
  140. //
  141. // actionButton
  142. //
  143. this.actionButton.Font = new System.Drawing.Font("Microsoft Sans Serif", 48F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  144. this.actionButton.Location = new System.Drawing.Point(12, 98);
  145. this.actionButton.Name = "actionButton";
  146. this.actionButton.Size = new System.Drawing.Size(260, 83);
  147. this.actionButton.TabIndex = 0;
  148. this.actionButton.Text = "Start";
  149. this.actionButton.UseVisualStyleBackColor = true;
  150. this.actionButton.Click += new System.EventHandler(this.actionButton_Click);
  151. //
  152. // timer
  153. //
  154. this.timer.Interval = 1000;
  155. this.timer.Tick += new System.EventHandler(this.timer_Tick);
  156. //
  157. // Form1
  158. //
  159. this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
  160. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  161. this.ClientSize = new System.Drawing.Size(284, 193);
  162. this.Controls.Add(this.actionButton);
  163. this.Controls.Add(this.timeBox);
  164. this.Name = "Form1";
  165. this.Text = "Egg Timer";
  166. this.ResumeLayout(false);
  167. this.PerformLayout();
  168.  
  169. }
  170.  
  171. #endregion
  172.  
  173. private System.Windows.Forms.TextBox timeBox;
  174. private System.Windows.Forms.Button actionButton;
  175. private System.Windows.Forms.Timer timer;
  176. }
  177. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.