Return to Snippet

Revision: 39041
at January 12, 2011 14:24 by mdmullinax


Updated Code
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);
            Application.Run(new Form1());
        }
    }

    public class Form1 : Form
    {
        private TimeSpan ts = new TimeSpan();

        public Form1()
        {
            InitializeComponent();
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            ts = ts.Subtract(new TimeSpan(0, 0, 1));
            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(':');
            ts = new TimeSpan(0, int.Parse(minSec[0]), int.Parse(minSec[1]));
            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.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
            this.timeBox = new System.Windows.Forms.TextBox();
            this.actionButton = new System.Windows.Forms.Button();
            this.timer = new System.Windows.Forms.Timer(this.components);
            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.Location = new System.Drawing.Point(12, 12);
            this.timeBox.Name = "timeBox";
            this.timeBox.Size = new System.Drawing.Size(260, 80);
            this.timeBox.TabIndex = 1;
            this.timeBox.Text = "03:00";
            this.timeBox.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            this.timeBox.TextChanged += new System.EventHandler(this.timeBox_TextChanged);
            this.timeBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.timeBox_KeyPress);
            // 
            // actionButton
            // 
            this.actionButton.Font = new System.Drawing.Font("Microsoft Sans Serif", 48F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.actionButton.Location = new System.Drawing.Point(12, 98);
            this.actionButton.Name = "actionButton";
            this.actionButton.Size = new System.Drawing.Size(260, 83);
            this.actionButton.TabIndex = 0;
            this.actionButton.Text = "Start";
            this.actionButton.UseVisualStyleBackColor = true;
            this.actionButton.Click += new System.EventHandler(this.actionButton_Click);
            // 
            // timer
            // 
            this.timer.Interval = 1000;
            this.timer.Tick += new System.EventHandler(this.timer_Tick);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 193);
            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;
    }
}

Revision: 39040
at January 12, 2011 14:22 by mdmullinax


Updated Code
using System;
using System.Windows.Forms;

namespace EggTimer
{
    public class Form1 : Form
    {
        private TimeSpan ts = new TimeSpan();

        public Form1()
        {
            InitializeComponent();
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            ts = ts.Subtract(new TimeSpan(0, 0, 1));
            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(':');
            ts = new TimeSpan(0, int.Parse(minSec[0]), int.Parse(minSec[1]));
            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.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
            this.timeBox = new System.Windows.Forms.TextBox();
            this.actionButton = new System.Windows.Forms.Button();
            this.timer = new System.Windows.Forms.Timer(this.components);
            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.Location = new System.Drawing.Point(12, 12);
            this.timeBox.Name = "timeBox";
            this.timeBox.Size = new System.Drawing.Size(260, 80);
            this.timeBox.TabIndex = 1;
            this.timeBox.Text = "03:00";
            this.timeBox.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            this.timeBox.TextChanged += new System.EventHandler(this.timeBox_TextChanged);
            this.timeBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.timeBox_KeyPress);
            // 
            // actionButton
            // 
            this.actionButton.Font = new System.Drawing.Font("Microsoft Sans Serif", 48F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.actionButton.Location = new System.Drawing.Point(12, 98);
            this.actionButton.Name = "actionButton";
            this.actionButton.Size = new System.Drawing.Size(260, 83);
            this.actionButton.TabIndex = 0;
            this.actionButton.Text = "Start";
            this.actionButton.UseVisualStyleBackColor = true;
            this.actionButton.Click += new System.EventHandler(this.actionButton_Click);
            // 
            // timer
            // 
            this.timer.Interval = 1000;
            this.timer.Tick += new System.EventHandler(this.timer_Tick);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 193);
            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;
    }
}

Revision: 39039
at January 12, 2011 14:21 by mdmullinax


Initial Code
using System;
using System.Windows.Forms;

namespace EggTimer
{
    public class Form1 : Form
    {
        private TimeSpan ts = new TimeSpan();

        public Form1()
        {
            InitializeComponent();
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            ts = ts.Subtract(new TimeSpan(0, 0, 1));
            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(':');
            ts = new TimeSpan(0, int.Parse(minSec[0]), int.Parse(minSec[1]));
            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 = "3: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.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
            this.timeBox = new System.Windows.Forms.TextBox();
            this.actionButton = new System.Windows.Forms.Button();
            this.timer = new System.Windows.Forms.Timer(this.components);
            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.Location = new System.Drawing.Point(12, 12);
            this.timeBox.Name = "timeBox";
            this.timeBox.Size = new System.Drawing.Size(260, 80);
            this.timeBox.TabIndex = 1;
            this.timeBox.Text = "03:00";
            this.timeBox.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
            this.timeBox.TextChanged += new System.EventHandler(this.timeBox_TextChanged);
            this.timeBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.timeBox_KeyPress);
            // 
            // actionButton
            // 
            this.actionButton.Font = new System.Drawing.Font("Microsoft Sans Serif", 48F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.actionButton.Location = new System.Drawing.Point(12, 98);
            this.actionButton.Name = "actionButton";
            this.actionButton.Size = new System.Drawing.Size(260, 83);
            this.actionButton.TabIndex = 0;
            this.actionButton.Text = "Start";
            this.actionButton.UseVisualStyleBackColor = true;
            this.actionButton.Click += new System.EventHandler(this.actionButton_Click);
            // 
            // timer
            // 
            this.timer.Interval = 1000;
            this.timer.Tick += new System.EventHandler(this.timer_Tick);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 193);
            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;
    }
}

Initial URL

                                

Initial Description
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

Initial Title
Egg Timer - c# winforms

Initial Tags

                                

Initial Language
C#