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.