Visual F# 100 Examples:Example Number 3

We are now on our third example of our planned 100 Visual F# windows form applications learning examples. If you noticed we are just using basic controls such as textboxes, labels, and buttons. We will add some new controls in our future examples. Meanwhile, try the following:

Problem: Create an application that will ask the name of an object or material, its voltage and current values then display its electrical resistance.

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 resistanceform=new Form(Text="Compute Resistance", Size=new System.Drawing.Size(300, 330),StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font)
let n1label=new Label(Text="Material:",Location=new System.Drawing.Point(0,20),AutoSize=true)
let materialtextbox=new TextBox(Location=new System.Drawing.Point(80, 20))
let n2label=new Label(Text="Current(volts):", Location=new System.Drawing.Point(0,50),AutoSize=true)
let currenttextbox=new TextBox(Location=new System.Drawing.Point(80,50))
let n3label=new Label(Text="Voltage(amperes):", Location=new System.Drawing.Point(0, 90),AutoSize=true)
let voltstextbox=new TextBox(Location=new System.Drawing.Point(130, 90))

let n4label=new Label(Text="Material:", Location=new System.Drawing.Point(0, 180),AutoSize=true)
let materiallabel=new Label(Location=new System.Drawing.Point(130, 180),BorderStyle=BorderStyle.FixedSingle)
let n5label=new Label(Text="Resistance(ohms):", Location=new System.Drawing.Point(0, 210),AutoSize=true)
let resistancelabel=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
resistanceform.Controls.Add(n1label)
resistanceform.Controls.Add(materialtextbox)
resistanceform.Controls.Add(n2label)
resistanceform.Controls.Add(currenttextbox)
resistanceform.Controls.Add(n3label)
resistanceform.Controls.Add(voltstextbox)
resistanceform.Controls.Add(n4label)
resistanceform.Controls.Add(materiallabel)
resistanceform.Controls.Add(n5label)
resistanceform.Controls.Add(resistancelabel)
resistanceform.Controls.Add(computebutton)
resistanceform.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 resistance using the formula resistance=voltage divided by current
//display the result on outcome on their assigned controls
let material=materialtextbox.Text
let current=Convert.ToDouble(currenttextbox.Text)
let voltage=Convert.ToDouble(voltstextbox.Text)
let resistance=voltage/current
materiallabel.Text<-material
resistancelabel.Text<-Convert.ToString(resistance))
 //when the exit button is clicked, close the form            
exitbutton.Click.Add(fun exit -> resistanceform.Close())  
Application.Run(resistanceform)

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)