Use Windows Forms in a .Net Core Class Library - .NET Core Control Library

I had a core 3.1 web app referring to a Framework 4.5.2 project that had dependencies on System.Windows.Forms. The fix for me was to add the below line to web app csproj file:

FrameworkReference Include="Microsoft.WindowsDesktop.App.WindowsForms"

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFrameworks>netcoreapp3.1</TargetFrameworks>
    <UseWindowsForms>true</UseWindowsForms>
  </PropertyGroup>

  <ItemGroup>
    <FrameworkReference Include="Microsoft.WindowsDesktop.App.WindowsForms" />
  </ItemGroup>

</Project>

In alternative you can add a new project of Windows Form Type and than set the output type to Libray.

<OutputType>Library</OutputType>

The currently accepted answer appears to be somewhat outdated. The recent syntax requires the target to be specified in the TargetFramework tag, not in the Sdk tag:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Library</OutputType>
    <TargetFramework>net5.0-windows</TargetFramework>
    <UseWindowsForms>true</UseWindowsForms>
    <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
  </PropertyGroup>

</Project>

More info here

  • https://docs.microsoft.com/en-us/dotnet/desktop/winforms/migration/?view=netdesktop-5.0
  • https://docs.microsoft.com/en-us/dotnet/standard/frameworks

Update: In later versions of VS 2019 (I tried Version 16.8.2) there is a project template for Windows Forms Control Library for .NET Core.

Currently Windows Forms .NET Core is in Preview mode and I don't know any official Nuget package or Project template for Windows Forms Control Library in .NET Core in VS 2019 16.2.2.

But to create a Windows Forms Control Library, you can use the following instructions:

  1. Add a new project of type Class Library (.NET Core)
  2. After project created, right click on project file and choose Edit Project File
  3. Change the project SDK to <Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
  4. Specify windows forms as UI technology by adding <UseWindowsForms>true</UseWindowsForms>.

Now the project file should be like the following:

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
  <PropertyGroup>
    <OutputType>Library</OutputType>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <UseWindowsForms>true</UseWindowsForms>
  </PropertyGroup>
</Project>

Now you can add Windows forms elements like Form or UserControl to this project and build the project without any problem.