Hope you guys had a look at my previous F# post

In this post i would be talking about using F# in a Windows Forms application. You should know that there is no designer support for F# so our necessity is to construct Form from  code. Standard Mechanishm like Data Binding work well with F# types.

  • Create a new F# application using Visual Studio named WinFormsHello
F# Project

F# Project

 

  • Go to the references of the application and add a reference to System.Windows.forms and System.Drawing
F# Add Reference

F# Add Reference

 

  • Lets open a namespace System.Windows.Forms. This will have the members of this namespace available for me here without having to reference them again and again.
Open Winforms Namespace in F#

Open Winforms Namespace in F#

 

  • Declare a simple datatype to work with. This particular data types are called as record in F# and when F# recognizes a data type it would use it just like a system data type.
  • Now we create an array of this record type that we just declared. You would see that semi colons are used as separators here. And if you would have worked in C# this declaration might seem a little different to you, so bear with it. The array starts with opening square bracket and a pipe [| and ends with pipe and a closing Square Bracket |].
  •  Now lets create a new form. Now you should keep in mind that F# wants you (as a convention) to use the new keyword for a type if that type implements iDisposable interface. Else we can leave the new keyword. Also you could see that i have passed a named parameter in Form instantiation. Now what this will do is create an instance of Form for me and then try to assign value to the Text Property of the form.
  • Now let’s create a new DataGrid and make its source as myEmployees collection. Also lets add the datagrid to the forms controls.
  • Now let’s pass the form as a parameter to Application.Run command.
WinForms HelloWorld

WinForms HelloWorld

 

  • When we run this application we are able to see our Form with the DataGridView
WinForms HelloWorld

WinForms HelloWorld

 

Any Feedback, comments or questions are always welcome.

 

Hey guys,

I am always exited to learn something new and hope you guys feel the same way. Today i am going to share as to how we can write a Hello World console application in F#. Hope you have VS 2010 installed.

  • Go to File –> New Project

  • Navigate to Other languages –> F# –> Windows –> F# Application
    New F# Project

    New F# Project

     

  •  If you open the references tab you could see a reference to FSharp.Core

 

FSharp.Core

FSharp.Core

  • Now write the following code in program.fs file. You will see that i have added comments along all the lines to explain what each line does.

    Code for F# Hello World Console App

    Code for F# Hello World Console App

  • Press control F5 and you would see the following screen

    Console Screen

    Console Screen

  • Enter some text and press Enter Key and you would see the screen below:

    Console Screen

    Console Screen

Any comments, feedback or questions are more than welcome.