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:)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.