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)  

No comments:

Post a Comment

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