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)

No comments:

Post a Comment

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