// TwoButtonStopWatch -- C# Version // This program simulates a two button stopwatch. // // Written by: Stuart Hansen // Last Modified: June 1, 2002 using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace TwoButtonStopwatch { public class Stopwatch : Form { private Container components; // The container for the buttons, etc. private State state; // The control state of the system private Button startStop; // The startStop button private Button lap; // The lap button private TextBox display; // The textbox where the time is displayed private Timer timer1; // The timer that sends ticks to the watch private long currentTime; // The internal time private long displayTime; // The time that is displayed // gets the currentTime private long getCurrentTime() { return currentTime; } // sets the current time private void setCurrentTime(long timeArg) { currentTime = timeArg; } // sets the display time private void setDisplayTime (long timeArg) { displayTime = timeArg; // Translate the minutes to a string String minutes = ((Int32)(displayTime/600)%60).ToString(); if ((displayTime/600)%60 < 10) minutes = "0" + minutes; // Translate the seconds to a string String seconds = ((Int32)(displayTime/10)%60).ToString(); if ((displayTime/10)%60 < 10) seconds = "0" + seconds; // Translate the tenths of seconds to a string String tenths = ((Int32)(displayTime%10)).ToString(); // Display the time display.Text = minutes + ":" + seconds + "." + tenths; } // The state interface that all concrete states must implement interface State { void updateTime(); // execute when the timer goes off void startStopClicked(); // execute when the stopStop button is clicked void lapClicked(); // execute when the lap button is clicked } // The state for a stopped watch class Stopped : State { private Stopwatch sw; // a reference to the stopwatch // A simple constructor public Stopped (Stopwatch swarg) { sw = swarg; } // Do nothing when the timer goes off and the watch is stopped public void updateTime() {} // Move to the started state public void startStopClicked() { sw.state = new Started(sw); } // Reset the watch public void lapClicked() { sw.setCurrentTime(0); sw.setDisplayTime(0); } } // The state when the watch is started class Started : State { private Stopwatch sw; // a reference to the stopwatch // A simple constructor public Started (Stopwatch swarg) { sw = swarg; } // Update the current time and display time on each timer tick public void updateTime() { sw.setCurrentTime(sw.getCurrentTime()+1); sw.setDisplayTime(sw.getCurrentTime()); } // Move to a stopped state public void startStopClicked() { sw.state = new Stopped(sw); } // Keep the watch running but freeze the display public void lapClicked() { sw.state = new LapAndStarted(sw); } } // The state when the watch is stopped and the display frozen class LapAndStopped : State { private Stopwatch sw; // a reference to the stopwatch // A simple constructor public LapAndStopped (Stopwatch swarg) { sw = swarg; } // Do nothing when the timer goes off public void updateTime() {} // Start the watch and keep the display frozen public void startStopClicked() { sw.state = new LapAndStarted(sw); } // Update the time and more to the stopped state public void lapClicked() { sw.setDisplayTime(sw.getCurrentTime()); sw.state = new Stopped(sw); } } // The state when the watch is running and the display is frozen class LapAndStarted : State { private Stopwatch sw; // a reference to the stopwatch // A simple constructor public LapAndStarted (Stopwatch swarg) { sw = swarg; } // Update the internal time, but keep the display frozen public void updateTime() { sw.setCurrentTime(sw.getCurrentTime()+1); } // Stop the watch and keep the display frozen public void startStopClicked() { sw.state = new LapAndStopped(sw); } // Update the display and unfreeze it. public void lapClicked() { sw.setDisplayTime(sw.getCurrentTime()); sw.state = new Started(sw); } } // The handler method for the lap button private void lap_Click(object sender, System.EventArgs e) { state.lapClicked(); } // The handler method for the startStop button private void startStop_Click(object sender, System.EventArgs e) { state.startStopClicked(); } // The handler method for the timer ticks private void timer1_Tick(object sender, System.EventArgs e) { state.updateTime(); } // The stopwatch constructor public Stopwatch() { // Build the stopwatch InitializeComponent(); // Set the initial state state = new Stopped (this); // Initialize the time attributes currentTime = 0; displayTime = 0; } // An automatically generated destructor protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } // An automatically generated GUI initializer private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.startStop = new System.Windows.Forms.Button(); this.lap = new System.Windows.Forms.Button(); this.display = new System.Windows.Forms.TextBox(); this.timer1 = new System.Windows.Forms.Timer(this.components); this.SuspendLayout(); // // startStop // this.startStop.Location = new System.Drawing.Point(40, 24); this.startStop.Name = "startStop"; this.startStop.Size = new System.Drawing.Size(120, 24); this.startStop.TabIndex = 0; this.startStop.Text = "Start/Stop"; this.startStop.Click += new System.EventHandler(this.startStop_Click); // // lap // this.lap.Location = new System.Drawing.Point(40, 72); this.lap.Name = "lap"; this.lap.Size = new System.Drawing.Size(120, 24); this.lap.TabIndex = 1; this.lap.Text = "Lap"; this.lap.Click += new System.EventHandler(this.lap_Click); // // display // this.display.Font = new System.Drawing.Font("Courier New", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.display.Location = new System.Drawing.Point(40, 120); this.display.Name = "display"; this.display.Size = new System.Drawing.Size(120, 22); this.display.TabIndex = 2; this.display.Text = "00:00.0"; this.display.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; // // timer1 // this.timer1.Enabled = true; this.timer1.Tick += new System.EventHandler(this.timer1_Tick); // // Stopwatch // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(192, 165); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.display, this.lap, this.startStop}); this.Name = "Stopwatch"; this.ResumeLayout(false); } // A very simple main static void Main() { Application.Run(new Stopwatch()); } } }