Add Two Numbers (Visual F# Windows Forms Application)

I’d been desperately combing the net for a while now looking for some tutorials relating to simple computations in Visual F# Windows Forms Application unfortunately I got no fluke finding some so I decided to make one. Today we will be making a simple application that adds two numbers and I hope that this will give you a head start in building advanced application using F# Windows Forms.
To create an application that adds two numbers, follow these simple steps:

1. Click Start>All Programs>Microsoft Visual Studio 2008>Microsoft Visual Studio 2008.

2. Click File>New>Project>Select Visual F# in the project types>Select F# application in the Visual Studio installed templates category.

3. Click the Project menu>Add reference>Click the .Net tab>Locate then double-click System.Windows.Forms.
Do step number 3 again and this time,select System.Drawing

4. Enter the following code after the line “// Learn more about F# at http://fsharp.net “:

  1. // Learn more about F# at http://fsharp.net  
  2. //uses the F# standard library  
  3. open System  
  4. //specifies the location of the Drawing classes  
  5. open System.Drawing  
  6. //specifies the namespace memory location of the form class  
  7. open System.Windows.Forms  
  8. //creates a new form  
  9. let addform=new Form(Text="Add Numbers", Size=new System.Drawing.Size(300, 200),StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font)  
  10. //creates a label  
  11. let n1label=new Label(Text="First number:",Top=20,Left=5,AutoSize=true)  
  12. let firsttextbox=new TextBox(Location=new System.Drawing.Point(80, 20))  
  13. //creates another label and change its text to “Second number:”  
  14. let n2label=new Label(Text="Second number:", Location=new System.Drawing.Point(0,50),AutoSize=true)  
  15. let secondtextbox=new TextBox(Location=new System.Drawing.Point(100,50))  
  16. //creates another label and change its text to sum  
  17. let n3label=new Label(Text="Sum:", Location=new System.Drawing.Point(0, 90),AutoSize=true)  
  18. //creates a label that will display the result of the computation  
  19. let anslabel=new Label(Location=new System.Drawing.Point(80, 90))  
  20. //make our buttons  
  21. let addbutton=new Button(Text="Add", Location=new System.Drawing.Point(100, 130))  
  22. let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(200, 130))  
  23. //add the controls into the form  
  24. addform.Controls.Add(n1label)  
  25. addform.Controls.Add(firsttextbox)  
  26. addform.Controls.Add(n2label)  
  27. addform.Controls.Add(secondtextbox)  
  28. addform.Controls.Add(n3label)  
  29. addform.Controls.Add(anslabel)  
  30. addform.Controls.Add(addbutton)  
  31. addform.Controls.Add(exitbutton)  
  32.   
  33. //when the add button is clicked  
  34. addbutton.Click.Add(fun addfunction ->  
  35. //convert the textbox values to unsigned int  
  36. let firstnum=Convert.ToUInt32(firsttextbox.Text)  
  37. let secondnum=Convert.ToUInt32(secondtextbox.Text)  
  38. let sumval=firstnum + secondnum  
  39. //display the sum in the anslabel  
  40. anslabel.Text<-Convert.ToString(sumval))  
  41.  //when the exit button is clicked, close the form              
  42. exitbutton.Click.Add(fun exit -> addform.Close())    
  43. addform.Show()  
  44. Application.Run(addform)  

6. Press Ctrl+F5 or click the run icon to execute your application. You should now see something similar to the screenshot below:



7. That's all. Enjoy programming!