Visual F# 100 Examples: Example Number 2

Last time I have shared to you our first simple example of window forms application using Visual F#, let’s now proceed to our second example:

Problem: Make a windows forms application that will ask the amount of consumption, amount of investment, government spending, amount of exports and imports and display the net emports and Gross Domestic Product (GDP).

Solution:
// 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 gdpform=new Form(Text="Compute GDP", Size=new System.Drawing.Size(300, 330),StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font)
let n1label=new Label(Text="Consumption:",Location=new System.Drawing.Point(0,20),AutoSize=true)
let firsttextbox=new TextBox(Location=new System.Drawing.Point(80, 20))
let n2label=new Label(Text="Investment:", Location=new System.Drawing.Point(0,50),AutoSize=true)
let secondtextbox=new TextBox(Location=new System.Drawing.Point(80,50))
let n3label=new Label(Text="Government spending:", Location=new System.Drawing.Point(0, 90),AutoSize=true)
let thirdtextbox=new TextBox(Location=new System.Drawing.Point(130, 90))
let n4label=new Label(Text="Exports:", Location=new System.Drawing.Point(0, 120),AutoSize=true)
let fourthtextbox=new TextBox(Location=new System.Drawing.Point(130, 120))
let n5label=new Label(Text="Imports:", Location=new System.Drawing.Point(0, 150),AutoSize=true)
let fifthtextbox=new TextBox(Location=new System.Drawing.Point(130, 150))
let n6label=new Label(Text="Imports:", Location=new System.Drawing.Point(0, 180),AutoSize=true)
let netemlabel=new Label(Location=new System.Drawing.Point(130, 180),BorderStyle=BorderStyle.FixedSingle)
let n7label=new Label(Text="GDP:", Location=new System.Drawing.Point(0, 210),AutoSize=true)
let gdplabel=new Label(Location=new System.Drawing.Point(130, 210),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
gdpform.Controls.Add(n1label)
gdpform.Controls.Add(firsttextbox)
gdpform.Controls.Add(n2label)
gdpform.Controls.Add(secondtextbox)
gdpform.Controls.Add(n3label)
gdpform.Controls.Add(thirdtextbox)
gdpform.Controls.Add(n4label)
gdpform.Controls.Add(fourthtextbox)
gdpform.Controls.Add(n5label)
gdpform.Controls.Add(fifthtextbox)
gdpform.Controls.Add(n6label)
gdpform.Controls.Add(netemlabel)
gdpform.Controls.Add(n7label)
gdpform.Controls.Add(gdplabel)
gdpform.Controls.Add(computebutton)
gdpform.Controls.Add(exitbutton)
//when the compute button is clicked
computebutton.Click.Add(fun compute->
//assigned the numbers inputted to each textbox to their corresponding variable
//compute the net emport using the formula net emport=export-import
//compute the gdp using the formula gdp=consumption+investment+govspending+netemport
let consumption=Convert.ToDouble(firsttextbox.Text)
let investment=Convert.ToDouble(secondtextbox.Text)
let govspending=Convert.ToDouble(thirdtextbox.Text)
let export=Convert.ToDouble(fourthtextbox.Text)
let import=Convert.ToDouble(fifthtextbox.Text)
let netemport=export-import
let gdp=(consumption+investment+govspending+netemport)
netemlabel.Text<-Convert.ToString(netemport)
                 gdplabel.Text<-Convert.ToString(gdp))
 //when the exit button is clicked, close the form            
exitbutton.Click.Add(fun exit -> gdpform.Close())  
Application.Run(gdpform)

Visual F# 100 Examples: Example Number 1

Starting today, I will be sharing to you guys some learning examples that will aid you in learning windows forms application programming in Visual F#. Our target is to be able to share at least 100 examples. Let’s start with example number 1:

Problem: Make a windows form application that will ask the user’s mental age and chronological age and display his intelligence quotient (IQ).

Solution:
// 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 iqform=new Form(Text="Compute IQ", Size=new System.Drawing.Size(300, 200),StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font)
//creates a label
let n1label=new Label(Text="Mental age:",Top=20,Left=5,AutoSize=true)
let firsttextbox=new TextBox(Location=new System.Drawing.Point(80, 20))
//creates another label and change its text to “Second number:”
let n2label=new Label(Text="Chronological age:", Location=new System.Drawing.Point(0,50),AutoSize=true)
let secondtextbox=new TextBox(Location=new System.Drawing.Point(100,50))
//creates another label and change its text to sum
let n3label=new Label(Text="IQ:", 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(80, 90), BorderStyle=BorderStyle.FixedSingle)
//make our buttons
let addbutton=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
iqform.Controls.Add(n1label)
iqform.Controls.Add(firsttextbox)
iqform.Controls.Add(n2label)
iqform.Controls.Add(secondtextbox)
iqform.Controls.Add(n3label)
iqform.Controls.Add(anslabel)
iqform.Controls.Add(addbutton)
iqform.Controls.Add(exitbutton)

//when the compute button is clicked
addbutton.Click.Add(fun addfunction ->
let manum=Convert.ToDouble(firsttextbox.Text)
let canum=Convert.ToDouble(secondtextbox.Text)
let iq=Convert.ToDouble((manum/canum)*100.00)

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