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:
  1. // Learn more about F# at http://fsharp.net  
  2. //specifies the memory location of the class files  
  3. //that will be needed in our application  
  4. open System.Collections.Generic  
  5. open System  
  6. open System.Windows.Forms  
  7. open System.ComponentModel  
  8. open System.Drawing  
  9. //creates our controls  
  10. let resistanceform=new Form(Text="Compute Resistance", Size=new System.Drawing.Size(300, 330),StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font)  
  11. let n1label=new Label(Text="Material:",Location=new System.Drawing.Point(0,20),AutoSize=true)  
  12. let materialtextbox=new TextBox(Location=new System.Drawing.Point(80, 20))  
  13. let n2label=new Label(Text="Current(volts):", Location=new System.Drawing.Point(0,50),AutoSize=true)  
  14. let currenttextbox=new TextBox(Location=new System.Drawing.Point(80,50))  
  15. let n3label=new Label(Text="Voltage(amperes):", Location=new System.Drawing.Point(0, 90),AutoSize=true)  
  16. let voltstextbox=new TextBox(Location=new System.Drawing.Point(130, 90))  
  17.   
  18. let n4label=new Label(Text="Material:", Location=new System.Drawing.Point(0, 180),AutoSize=true)  
  19. let materiallabel=new Label(Location=new System.Drawing.Point(130, 180),BorderStyle=BorderStyle.FixedSingle)  
  20. let n5label=new Label(Text="Resistance(ohms):", Location=new System.Drawing.Point(0, 210),AutoSize=true)  
  21. let resistancelabel=new Label(Location=new System.Drawing.Point(130, 210),BorderStyle=BorderStyle.FixedSingle)  
  22. let computebutton=new Button(Text="Compute", Location=new System.Drawing.Point(100, 250))  
  23. let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(200, 250))  
  24.   
  25. //add the controls into the form  
  26. resistanceform.Controls.Add(n1label)  
  27. resistanceform.Controls.Add(materialtextbox)  
  28. resistanceform.Controls.Add(n2label)  
  29. resistanceform.Controls.Add(currenttextbox)  
  30. resistanceform.Controls.Add(n3label)  
  31. resistanceform.Controls.Add(voltstextbox)  
  32. resistanceform.Controls.Add(n4label)  
  33. resistanceform.Controls.Add(materiallabel)  
  34. resistanceform.Controls.Add(n5label)  
  35. resistanceform.Controls.Add(resistancelabel)  
  36. resistanceform.Controls.Add(computebutton)  
  37. resistanceform.Controls.Add(exitbutton)  
  38.   
  39. //when the compute button is clicked  
  40. computebutton.Click.Add(fun compute->  
  41. //assign the data inputted in each control to each corresponding variables  
  42. //compute the resistance using the formula resistance=voltage divided by current  
  43. //display the result on outcome on their assigned controls  
  44. let material=materialtextbox.Text  
  45. let current=Convert.ToDouble(currenttextbox.Text)  
  46. let voltage=Convert.ToDouble(voltstextbox.Text)  
  47. let resistance=voltage/current  
  48. materiallabel.Text<-material  
  49. resistancelabel.Text<-Convert.ToString(resistance))  
  50.  //when the exit button is clicked, close the form              
  51. exitbutton.Click.Add(fun exit -> resistanceform.Close())    
  52. 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:
  1. // Learn more about F# at http://fsharp.net  
  2. //specifies the memory location of the class files  
  3. //that will be needed in our application  
  4. open System.Collections.Generic  
  5. open System  
  6. open System.Windows.Forms  
  7. open System.ComponentModel  
  8. open System.Drawing  
  9. //creates our controls  
  10. let gdpform=new Form(Text="Compute GDP", Size=new System.Drawing.Size(300, 330),StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font)  
  11. let n1label=new Label(Text="Consumption:",Location=new System.Drawing.Point(0,20),AutoSize=true)  
  12. let firsttextbox=new TextBox(Location=new System.Drawing.Point(80, 20))  
  13. let n2label=new Label(Text="Investment:", Location=new System.Drawing.Point(0,50),AutoSize=true)  
  14. let secondtextbox=new TextBox(Location=new System.Drawing.Point(80,50))  
  15. let n3label=new Label(Text="Government spending:", Location=new System.Drawing.Point(0, 90),AutoSize=true)  
  16. let thirdtextbox=new TextBox(Location=new System.Drawing.Point(130, 90))  
  17. let n4label=new Label(Text="Exports:", Location=new System.Drawing.Point(0, 120),AutoSize=true)  
  18. let fourthtextbox=new TextBox(Location=new System.Drawing.Point(130, 120))  
  19. let n5label=new Label(Text="Imports:", Location=new System.Drawing.Point(0, 150),AutoSize=true)  
  20. let fifthtextbox=new TextBox(Location=new System.Drawing.Point(130, 150))  
  21. let n6label=new Label(Text="Imports:", Location=new System.Drawing.Point(0, 180),AutoSize=true)  
  22. let netemlabel=new Label(Location=new System.Drawing.Point(130, 180),BorderStyle=BorderStyle.FixedSingle)  
  23. let n7label=new Label(Text="GDP:", Location=new System.Drawing.Point(0, 210),AutoSize=true)  
  24. let gdplabel=new Label(Location=new System.Drawing.Point(130, 210),BorderStyle=BorderStyle.FixedSingle)  
  25. let computebutton=new Button(Text="Compute", Location=new System.Drawing.Point(100, 250))  
  26. let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(200, 250))  
  27. //add the controls into the form  
  28. gdpform.Controls.Add(n1label)  
  29. gdpform.Controls.Add(firsttextbox)  
  30. gdpform.Controls.Add(n2label)  
  31. gdpform.Controls.Add(secondtextbox)  
  32. gdpform.Controls.Add(n3label)  
  33. gdpform.Controls.Add(thirdtextbox)  
  34. gdpform.Controls.Add(n4label)  
  35. gdpform.Controls.Add(fourthtextbox)  
  36. gdpform.Controls.Add(n5label)  
  37. gdpform.Controls.Add(fifthtextbox)  
  38. gdpform.Controls.Add(n6label)  
  39. gdpform.Controls.Add(netemlabel)  
  40. gdpform.Controls.Add(n7label)  
  41. gdpform.Controls.Add(gdplabel)  
  42. gdpform.Controls.Add(computebutton)  
  43. gdpform.Controls.Add(exitbutton)  
  44. //when the compute button is clicked  
  45. computebutton.Click.Add(fun compute->  
  46. //assigned the numbers inputted to each textbox to their corresponding variable  
  47. //compute the net emport using the formula net emport=export-import  
  48. //compute the gdp using the formula gdp=consumption+investment+govspending+netemport  
  49. let consumption=Convert.ToDouble(firsttextbox.Text)  
  50. let investment=Convert.ToDouble(secondtextbox.Text)  
  51. let govspending=Convert.ToDouble(thirdtextbox.Text)  
  52. let export=Convert.ToDouble(fourthtextbox.Text)  
  53. let import=Convert.ToDouble(fifthtextbox.Text)  
  54. let netemport=export-import  
  55. let gdp=(consumption+investment+govspending+netemport)  
  56. netemlabel.Text<-Convert.ToString(netemport)  
  57.                  gdplabel.Text<-Convert.ToString(gdp))  
  58.  //when the exit button is clicked, close the form              
  59. exitbutton.Click.Add(fun exit -> gdpform.Close())    
  60. Application.Run(gdpform)