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)