{"id":2228,"date":"2014-07-28T21:36:53","date_gmt":"2014-07-28T21:36:53","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=2228"},"modified":"2014-07-28T21:36:53","modified_gmt":"2014-07-28T21:36:53","slug":"creating-an-f-winforms-application","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/creating-an-f-winforms-application\/","title":{"rendered":"Creating an F# WinForms application"},"content":{"rendered":"<p>I wanted to mess around with the <a href=\"http:\/\/fsharp.github.io\/FSharp.Charting\/index.html\" title=\"F# Charting: Library for Data Visualization\" target=\"_blank\">FSharp.Charting<\/a> library and needed to create an F# WinForms application to use it.<\/p>\n<p>It&#8217;s pretty simple but there&#8217;s no template by default in Visual Studio (well not in 2013). <\/p>\n<p>So here the steps to create a WinForms F# application<\/p>\n<ul>\n<li>Create a new Project and select F# Application<\/li>\n<li>This will be a console application, so next select the project properties and change the Application | Output type from Console Application to Windows Application<\/li>\n<li>Add a reference to System.Windows.Forms<\/li>\n<li>Change your main function to look like the following\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nopen System\r\nopen System.Windows.Forms\r\n\r\n&#x5B;&lt;EntryPoint&gt;]\r\n&#x5B;&lt;STAThread&gt;]\r\nlet main argv = \r\n\r\n    Application.EnableVisualStyles()\r\n    Application.SetCompatibleTextRenderingDefault false\r\n\r\n    use form = new Form()\r\n\r\n    Application.Run(form);\r\n\r\n    0 \r\n<\/pre>\n<\/li>\n<\/ul>\n<p>Obviously this example shows us creating an empty Form. By default F# (at least in Visual Studio 2013) doesn&#8217;t include WinForm design surfaces or the likes. So it&#8217;s probably best to design your forms in a C# library and reference from your F# applications. Or, alternatively, you can hand code your WinForms.<\/p>\n<p><strong>A quick example of using FSharp.Charting<\/strong><\/p>\n<p>This is not mean&#8217;t to be anything other than a quick example, but as I wanted to get a F# WinForms application specifically to try out some FSharp.Charting, here&#8217;s the steps&#8230;<\/p>\n<ul>\n<li>Using NuGet add a reference to FSharp.Charting to your project (developed above)<\/li>\n<li>Add <em>open FSharp.Charting<\/em> to your Program.fs file<\/li>\n<li>I&#8217;m going to simple use some code from the FSharp.Charting github samples, so first we need to add a reference to System.Drawing<\/li>\n<li>Add a reference to System.Windows.Forms.DataVisualization<\/li>\n<li>Finally change the Application.Run method to look like the following\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nApplication.Run (Chart.Line(&#x5B; for x in 0 .. 10 -&gt; x, x*x ]).ShowChart())\r\n<\/pre>\n<\/li>\n<\/ul>\n<p>Now your code should look like the following<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nopen System\r\nopen System.Windows.Forms\r\nopen FSharp.Charting\r\n\r\n&#x5B;&lt;EntryPoint&gt;]\r\n&#x5B;&lt;STAThread&gt;]\r\nlet main argv = \r\n\r\n    Application.EnableVisualStyles()\r\n    Application.SetCompatibleTextRenderingDefault false\r\n\r\n    Application.Run (Chart.Line(&#x5B; for x in 0 .. 10 -&gt; x, x*x ]).ShowChart())\r\n\r\n    0\r\n<\/pre>\n<p>and this should (when run) display a line chart.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I wanted to mess around with the FSharp.Charting library and needed to create an F# WinForms application to use it. It&#8217;s pretty simple but there&#8217;s no template by default in Visual Studio (well not in 2013). So here the steps to create a WinForms F# application Create a new Project and select F# Application This [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[6],"tags":[],"class_list":["post-2228","post","type-post","status-publish","format-standard","hentry","category-f"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/2228","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=2228"}],"version-history":[{"count":9,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/2228\/revisions"}],"predecessor-version":[{"id":2240,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/2228\/revisions\/2240"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=2228"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=2228"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=2228"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}