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 “:
// Learn more about F# at http://fsharp.net //uses the F# standard library open System //specifies the location of the Drawing classes open System.Drawing //specifies the namespace memory location of the form class open System.Windows.Forms //creates a new form let addform=new Form(Text="Add Numbers", Size=new System.Drawing.Size(300, 200),StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font) //creates a label let n1label=new Label(Text="First number:",Top=20,Left=5,AutoSize=true) let firsttextbox=new TextBox(Location=new System.Drawing.Point(80, 20)) //creates another label and change its text to “Second number:” let n2label=new Label(Text="Second number:", Location=new System.Drawing.Point(0,50),AutoSize=true) let secondtextbox=new TextBox(Location=new System.Drawing.Point(100,50)) //creates another label and change its text to sum let n3label=new Label(Text="Sum:", Location=new System.Drawing.Point(0, 90),AutoSize=true) //creates a label that will display the result of the computation let anslabel=new Label(Location=new System.Drawing.Point(80, 90)) //make our buttons let addbutton=new Button(Text="Add", Location=new System.Drawing.Point(100, 130)) let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(200, 130)) //add the controls into the form addform.Controls.Add(n1label) addform.Controls.Add(firsttextbox) addform.Controls.Add(n2label) addform.Controls.Add(secondtextbox) addform.Controls.Add(n3label) addform.Controls.Add(anslabel) addform.Controls.Add(addbutton) addform.Controls.Add(exitbutton) //when the add button is clicked addbutton.Click.Add(fun addfunction -> //convert the textbox values to unsigned int let firstnum=Convert.ToUInt32(firsttextbox.Text) let secondnum=Convert.ToUInt32(secondtextbox.Text) let sumval=firstnum + secondnum //display the sum in the anslabel anslabel.Text<-Convert.ToString(sumval)) //when the exit button is clicked, close the form exitbutton.Click.Add(fun exit -> addform.Close()) addform.Show() 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!