Introduction
This article is an explanation of how to create a Windows Forms application in F#. The Windows Forms classes are contained in System.Drawing.dll and System.Forms.Drawing.dll. The libraries are based on the System.Windows.Forms.Form class which shows a Window visible to the user.
Steps to Create Windows Application
-
Open Visual Studio 2010; select the Menu File, then select new and click on Project (File->New->Project). A New project Dialog box will open. This will show the available installed templates in Visual Studio. Select Visual F# Template from the Installed Templates like in the following Image.
![Windows form application in F#](https://www.c-sharpcorner.com/UploadFile/f5b919/windows-form-application-in-fsharp/Images/Winform1.gif)
-
Now expand the Visual F# template and select Windows than choose F# window app (winform). If this is not installed in your Visual Studio then you can install it from here.
http://www.c-sharpcorner.com/Resources/1201/f-window-app-winform-template.aspx
When you install this template the New Project dialog box will display like below.
![Windows form application in F#](https://www.c-sharpcorner.com/UploadFile/f5b919/windows-form-application-in-fsharp/Images/winform2.gif)
-
Now give a name to your project in the given TextBox and click on OK Button.
![Windows form application in F#](https://www.c-sharpcorner.com/UploadFile/f5b919/windows-form-application-in-fsharp/Images/winform6.gif)
-
Your Solution Explorer will look like below.
![Solution explorer](https://www.c-sharpcorner.com/UploadFile/f5b919/windows-form-application-in-fsharp/Images/winform3.gif)
-
Now write the following code in the Program.fs file.
Example
The Program.fs window will look like the following:
![Example of Winform](https://www.c-sharpcorner.com/UploadFile/f5b919/windows-form-application-in-fsharp/Images/winform4.gif)
namespace Firstform
open System
open System.Drawing
open System.Windows.Forms
module Main =
//to add label in form
let label =
let temp = new Label()
do temp.Size <- new System.Drawing.Size(100,100)
do temp.Text<- "My First F# windows application!"
do temp.AutoSize <- true
do temp.Location <- new Drawing.Point(25,25)
temp
//to creat a form
let form = new Form(BackColor = Color.Pink, Text = "First Window App")
form.Controls.Add(label)
Application.Run(form)
6. Now Press f5 to execute the program.
Output
![Output of winform](https://www.c-sharpcorner.com/UploadFile/f5b919/windows-form-application-in-fsharp/Images/winform5.gif)
Summary
In this article I have discussed how to create a Windows Forms application in F#.