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)