Skip to content

Instantly share code, notes, and snippets.

@nagat01
Created June 25, 2011 09:42
Show Gist options
  • Save nagat01/1046321 to your computer and use it in GitHub Desktop.
Save nagat01/1046321 to your computer and use it in GitHub Desktop.
Windows form application of stopwatch in 30 lines of code in F#
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