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)