Windows Form Application in F#


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

  1. 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#

  2. 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#

  3. Now give a name to your project in the given TextBox and click on OK Button.

    Windows form application in F#

  4. Your Solution Explorer will look like below.

    Solution explorer

  5. Now write the following code in the Program.fs file.

Example

The Program.fs window will look like the following:

Example of Winform

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

Summary

In this article I have discussed how to create a Windows Forms application in F#.


Similar Articles