Visual F# 100 Examples: Example Number 4

Problem: Make an application that will display the area of an annulus (overlapping circles). The area of an annulus can be calculated by subtracting the area of a small circle from the area of a larger circle.

// Learn more about F# at http://fsharp.net
//specifies the memory location of the class files
//that will be needed in our application
open System.Collections.Generic
open System
open System.Windows.Forms
open System.ComponentModel
open System.Drawing
//creates a new form
let annulform=new Form(Text="Dislay the Area of an Annulus", Size=new System.Drawing.Size(300, 200),StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font)
//creates our controls
let n1label=new Label(Text="Area of a large circle:",Location=new System.Drawing.Point(0,20),AutoSize=true)
let firsttextbox=new TextBox(Location=new System.Drawing.Point(120, 20))
let n2label=new Label(Text="Area of a small circle:", Location=new System.Drawing.Point(0,50),AutoSize=true)
let secondtextbox=new TextBox(Location=new System.Drawing.Point(120,50))
let n3label=new Label(Text="Area of an annulus:", Location=new System.Drawing.Point(0, 90),AutoSize=true)
//creates a label that will display the result of the computation
let anslabel=new Label(Location=new System.Drawing.Point(120, 90), BorderStyle=BorderStyle.FixedSingle)
//make our buttons
let computebutton=new Button(Text="Compute", Location=new System.Drawing.Point(100, 130))
let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(200, 130))
//add the controls into the form
annulform.Controls.Add(n1label)
annulform.Controls.Add(firsttextbox)
annulform.Controls.Add(n2label)
annulform.Controls.Add(secondtextbox)
annulform.Controls.Add(n3label)
annulform.Controls.Add(anslabel)
annulform.Controls.Add(computebutton)
annulform.Controls.Add(exitbutton)

//when the compute button is clicked
computebutton.Click.Add(fun ans ->
let areaoflargecircle=Convert.ToDouble(firsttextbox.Text)
let areaofsmallcircle=Convert.ToDouble(secondtextbox.Text)
let areaofannulus=areaoflargecircle-areaofsmallcircle

//display the area of annulus value in the anslabel
anslabel.Text<-Convert.ToString(areaofannulus))
 //when the exit button is clicked, close the form            
exitbutton.Click.Add(fun exit -> annulform.Close())  
Application.Run(annulform)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.