Recently I have been doing some work with FSharp.Charting and found there was no simple template for an F# WinForms application. So to remedy this situation I have posted one on the Visual Studio Gallery:

When the template is installed you get the following template added to your F# folder when creating a new F# project:


So what does this template do? The template creates a new project consisting of two F# files, MyForm.fs and Program.fs.

The MyForm.fs file provides the structure for a type derived from Form. It demonstrates the Form definition providing placeholders for adding forms to the control and hooking up both form and control events.

namespace FSharp.WinForms

open System
open System.Drawing
open System.Windows.Forms

type public MyForm() as form =
    inherit Form()

    // TODO define your controls
    let formLabel = new Label()
    // TODO initialize your controls
    let initControls =
        formLabel.Text <- "My form data"
        formLabel.DoubleClick.AddHandler(new EventHandler
            (fun sender e -> form.eventLabel_DoubleClick(sender, e)))   


        // TODO add controls to the form

        // TODO define form properties
        form.ClientSize <- new Size(600, 600)
        form.Text <- "F# Form"

        // TODO define any required for events
        form.Load.AddHandler(new System.EventHandler
            (fun sender e -> form.eventForm_Loading(sender, e)))

        // render the form

    // TODO define your event handlers
    member form.eventForm_Loading(sender:obj, e:EventArgs) =

    member form.eventLabel_DoubleClick(sender:obj, e:EventArgs) =

As you can see TODO comments define where the code needs to be modified to build up your form application.

The Program.fs file, which can be left unchanged, loads the form on the appropriate STA thread:

namespace FSharp.WinForms

open System
open System.Drawing
open System.Windows.Forms

module Main =

        Application.Run(new MyForm() :> Form)

Hopefully you will find this template useful.

Written by Carl Nolan