Visual F# 100 Examples: Example Number 6

Problem. Create an application that will display the surface area of a cube. The surface area of a cube can be computed by multiplying the inputted side by itself and multiplying the product by 6.
  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 a new form  
  10. let cubeform=new Form(Text="Compute the Surface Area of a Cube", Size=new System.Drawing.Size(300, 200),StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font)  
  11. //creates our controls  
  12. let n1label=new Label(Text="Enter a side value:",Location=new System.Drawing.Point(0,20),AutoSize=true)  
  13. let firsttextbox=new TextBox(Location=new System.Drawing.Point(120, 20))  
  14. let n2label=new Label(Text="Surface area of a cube:", Location=new System.Drawing.Point(0, 90),AutoSize=true)  
  15. //creates a label that will display the result of the computation  
  16. let anslabel=new Label(Location=new System.Drawing.Point(140, 90), BorderStyle=BorderStyle.FixedSingle)  
  17. //make our buttons  
  18. let computebutton=new Button(Text="Compute", Location=new System.Drawing.Point(100, 130))  
  19. let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(200, 130))  
  20. //add the controls into the form  
  21. cubeform.Controls.Add(n1label)  
  22. cubeform.Controls.Add(firsttextbox)  
  23. cubeform.Controls.Add(n2label)  
  24. cubeform.Controls.Add(anslabel)  
  25. cubeform.Controls.Add(computebutton)  
  26. cubeform.Controls.Add(exitbutton)  
  27.   
  28. //when the compute button is clicked  
  29. computebutton.Click.Add(fun ans->  
  30. let sidevalue=Convert.ToDouble(firsttextbox.Text)  
  31. let areaofacube=6.00*(sidevalue*sidevalue)  
  32.   
  33. //display the areaofacube value in the anslabel  
  34. anslabel.Text<-Convert.ToString(areaofacube))  
  35.  //when the exit button is clicked, close the form              
  36. exitbutton.Click.Add(fun exit -> cubeform.Close())    
  37. Application.Run(cubeform)  

We'll be adding some new controls after our tenth example:)

Visual F# 100 Examples:Example Number 5

Problem: Design an application that will ask a length value, height value, and width value and display the volume of the prism.
  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 prismform=new Form(Text="Compute the Volume of a Prism", Size=new System.Drawing.Size(300, 330),StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font)  
  11. let n1label=new Label(Text="Length value:",Location=new System.Drawing.Point(0,20),AutoSize=true)  
  12. let lenghttxtbox=new TextBox(Location=new System.Drawing.Point(80, 20))  
  13. let n2label=new Label(Text="Width value:", Location=new System.Drawing.Point(0,50),AutoSize=true)  
  14. let widthtxtbox=new TextBox(Location=new System.Drawing.Point(80,50))  
  15. let n3label=new Label(Text="Height value:", Location=new System.Drawing.Point(0, 90),AutoSize=true)  
  16. let heighttxtbox=new TextBox(Location=new System.Drawing.Point(130, 90))  
  17.   
  18. let n4label=new Label(Text="Prisms volume:", Location=new System.Drawing.Point(0, 180),AutoSize=true)  
  19. let prismlabel=new Label(Location=new System.Drawing.Point(130, 180),BorderStyle=BorderStyle.FixedSingle)  
  20. let computebutton=new Button(Text="Compute", Location=new System.Drawing.Point(100, 250))  
  21. let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(200, 250))  
  22.   
  23. //add the controls into the form  
  24. prismform.Controls.Add(n1label)  
  25. prismform.Controls.Add(lenghttxtbox)  
  26. prismform.Controls.Add(n2label)  
  27. prismform.Controls.Add(widthtxtbox)  
  28. prismform.Controls.Add(n3label)  
  29. prismform.Controls.Add(heighttxtbox)  
  30. prismform.Controls.Add(n4label)  
  31. prismform.Controls.Add(prismlabel)  
  32. prismform.Controls.Add(computebutton)  
  33. prismform.Controls.Add(exitbutton)  
  34.   
  35. //when the compute button is clicked  
  36. computebutton.Click.Add(fun compute->  
  37. //assign the data inputted in each control to each corresponding variables  
  38. //compute the volume using the formula volume=lengthvalue*widthvalue*heightvalue  
  39. //display the result on outcome on their assigned controls  
  40. let lenghtvalue=Convert.ToDouble(lenghttxtbox.Text)  
  41. let widthvalue=Convert.ToDouble(widthtxtbox.Text)  
  42. let heightvalue=Convert.ToDouble(heighttxtbox.Text)  
  43. let prismvol=lenghtvalue*widthvalue*heightvalue  
  44. prismlabel.Text<-Convert.ToString(prismvol))  
  45.  //when the exit button is clicked, close the form              
  46. exitbutton.Click.Add(fun exit -> prismform.Close())    
  47. Application.Run(prismform)  

Visual F# 100 Examples: Example Number 4

Problem: Make an application that will display the area of an annulus (overlapping circles). The area of an annulus can be calculated by subtracting the area of a small circle from the area of a larger circle.

  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 a new form  
  10. let annulform=new Form(Text="Dislay the Area of an Annulus", Size=new System.Drawing.Size(300, 200),StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font)  
  11. //creates our controls  
  12. let n1label=new Label(Text="Area of a large circle:",Location=new System.Drawing.Point(0,20),AutoSize=true)  
  13. let firsttextbox=new TextBox(Location=new System.Drawing.Point(120, 20))  
  14. let n2label=new Label(Text="Area of a small circle:", Location=new System.Drawing.Point(0,50),AutoSize=true)  
  15. let secondtextbox=new TextBox(Location=new System.Drawing.Point(120,50))  
  16. let n3label=new Label(Text="Area of an annulus:", Location=new System.Drawing.Point(0, 90),AutoSize=true)  
  17. //creates a label that will display the result of the computation  
  18. let anslabel=new Label(Location=new System.Drawing.Point(120, 90), BorderStyle=BorderStyle.FixedSingle)  
  19. //make our buttons  
  20. let computebutton=new Button(Text="Compute", Location=new System.Drawing.Point(100, 130))  
  21. let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(200, 130))  
  22. //add the controls into the form  
  23. annulform.Controls.Add(n1label)  
  24. annulform.Controls.Add(firsttextbox)  
  25. annulform.Controls.Add(n2label)  
  26. annulform.Controls.Add(secondtextbox)  
  27. annulform.Controls.Add(n3label)  
  28. annulform.Controls.Add(anslabel)  
  29. annulform.Controls.Add(computebutton)  
  30. annulform.Controls.Add(exitbutton)  
  31.   
  32. //when the compute button is clicked  
  33. computebutton.Click.Add(fun ans ->  
  34. let areaoflargecircle=Convert.ToDouble(firsttextbox.Text)  
  35. let areaofsmallcircle=Convert.ToDouble(secondtextbox.Text)  
  36. let areaofannulus=areaoflargecircle-areaofsmallcircle  
  37.   
  38. //display the area of annulus value in the anslabel  
  39. anslabel.Text<-Convert.ToString(areaofannulus))  
  40.  //when the exit button is clicked, close the form              
  41. exitbutton.Click.Add(fun exit -> annulform.Close())    
  42. Application.Run(annulform)