Visual F# 100 Examples: Example Number 6

Problem. Create an application that will display the surface area of a cube. The surface area of a cube can be computed by multiplying the inputted side by itself and multiplying the product by 6.
// 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 cubeform=new Form(Text="Compute the Surface Area of a Cube", Size=new System.Drawing.Size(300, 200),StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font)
//creates our controls
let n1label=new Label(Text="Enter a side value:",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="Surface area of a cube:", 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(140, 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
cubeform.Controls.Add(n1label)
cubeform.Controls.Add(firsttextbox)
cubeform.Controls.Add(n2label)
cubeform.Controls.Add(anslabel)
cubeform.Controls.Add(computebutton)
cubeform.Controls.Add(exitbutton)

//when the compute button is clicked
computebutton.Click.Add(fun ans->
let sidevalue=Convert.ToDouble(firsttextbox.Text)
let areaofacube=6.00*(sidevalue*sidevalue)

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

We'll be adding some new controls after our tenth example:)

Visual F# 100 Examples:Example Number 5

Problem: Design an application that will ask a length value, height value, and width value and display the volume of the prism.
// 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 our controls
let prismform=new Form(Text="Compute the Volume of a Prism", Size=new System.Drawing.Size(300, 330),StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font)
let n1label=new Label(Text="Length value:",Location=new System.Drawing.Point(0,20),AutoSize=true)
let lenghttxtbox=new TextBox(Location=new System.Drawing.Point(80, 20))
let n2label=new Label(Text="Width value:", Location=new System.Drawing.Point(0,50),AutoSize=true)
let widthtxtbox=new TextBox(Location=new System.Drawing.Point(80,50))
let n3label=new Label(Text="Height value:", Location=new System.Drawing.Point(0, 90),AutoSize=true)
let heighttxtbox=new TextBox(Location=new System.Drawing.Point(130, 90))

let n4label=new Label(Text="Prisms volume:", Location=new System.Drawing.Point(0, 180),AutoSize=true)
let prismlabel=new Label(Location=new System.Drawing.Point(130, 180),BorderStyle=BorderStyle.FixedSingle)
let computebutton=new Button(Text="Compute", Location=new System.Drawing.Point(100, 250))
let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(200, 250))

//add the controls into the form
prismform.Controls.Add(n1label)
prismform.Controls.Add(lenghttxtbox)
prismform.Controls.Add(n2label)
prismform.Controls.Add(widthtxtbox)
prismform.Controls.Add(n3label)
prismform.Controls.Add(heighttxtbox)
prismform.Controls.Add(n4label)
prismform.Controls.Add(prismlabel)
prismform.Controls.Add(computebutton)
prismform.Controls.Add(exitbutton)

//when the compute button is clicked
computebutton.Click.Add(fun compute->
//assign the data inputted in each control to each corresponding variables
//compute the volume using the formula volume=lengthvalue*widthvalue*heightvalue
//display the result on outcome on their assigned controls
let lenghtvalue=Convert.ToDouble(lenghttxtbox.Text)
let widthvalue=Convert.ToDouble(widthtxtbox.Text)
let heightvalue=Convert.ToDouble(heighttxtbox.Text)
let prismvol=lenghtvalue*widthvalue*heightvalue
prismlabel.Text<-Convert.ToString(prismvol))
 //when the exit button is clicked, close the form            
exitbutton.Click.Add(fun exit -> prismform.Close())  
Application.Run(prismform)