isaacabraham / get-programming-fsharp

Companion code repository for https://www.manning.com/books/get-programming-with-f-sharp
125 stars 57 forks source link

System.Windows.Forms is not defined #53

Open james-aslett opened 1 year ago

james-aslett commented 1 year ago

I have reached Listing 4.8, and was unable to get System.Windows.Forms working in my .NET Core 3.1 project, so I downloaded this repo. Unfortunately, the same is true here - if I go to folder view > src > code-listings > lesson-04.fsx and try to run Listing 4.8 in FSI I get 'The namespace 'Forms' is not defined.' I've already run build.cmd. How can I get Forms to play nice in this project? err

MaxGripe commented 1 year ago

With support from FSharp channel on Discord, we managed to make it work with the following code:

open System
open System.Net
open System.Windows.Forms

[<EntryPoint; STAThread>]
let main argv =    
    let webClient = new WebClient()
    let fsharpOrg = webClient.DownloadString(Uri "http://fsharp.org")
    let browser =
        new WebBrowser(ScriptErrorsSuppressed = true,
                       Dock = DockStyle.Fill,
                       DocumentText = fsharpOrg)

    let form = new Form(Text = "Hello from F#!")
    form.Controls.Add browser
    form.Show()
    Application.Run(form)
    0

and .fsproj settings:

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
    <PropertyGroup>
        <OutputType>WinExe</OutputType>
        <TargetFramework>net7.0-windows</TargetFramework>
        <UseWindowsForms>true</UseWindowsForms>
        <LangVersion>7.0</LangVersion>
    </PropertyGroup>
    <ItemGroup>
        <Compile Include="Program.fs" />
    </ItemGroup>
</Project>