Created
June 25, 2011 09:42
-
-
Save nagat01/1046321 to your computer and use it in GitHub Desktop.
Windows form application of stopwatch in 30 lines of code in F#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open System | |
open System.Drawing | |
open System.Windows.Forms | |
let mutable startTime = new DateTime() | |
let timerLabel = new Label(Text="0",Location=Point(0,0)) | |
let startButton = new Button(Text="Start",Location=Point(0,24)) | |
let stopButton = new Button(Text="Stop",Location=Point(0,48)) | |
let clearButton = new Button(Text="Clear",Location=Point(0,72)) | |
let timer = new Timer(Interval=20) | |
timer.Tick.Add (fun _ -> | |
timerLabel.Text <- string <| DateTime.Now - startTime) | |
startButton.Click.Add begin fun _ -> | |
if String.IsNullOrEmpty timerLabel.Text then | |
startTime <- DateTime.Now | |
else | |
startTime <- DateTime.Now - TimeSpan.Parse timerLabel.Text | |
timer.Start() | |
end | |
stopButton.Click.Add (fun _ ->timer.Stop()) | |
clearButton.Click.Add (fun _ -> | |
timer.Stop() | |
timerLabel.Text <- "") | |
let form = new Form(Text="Stop Watch") | |
form.Controls.AddRange [|timerLabel;startButton;stopButton;clearButton|] | |
Application.Run(form) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment