Resolved Create Net 5 UserControl

aaaron123

Member
Joined
Jan 3, 2021
Messages
18
Programming Experience
1-3
I'm trying Net 5 carefully.

I created a Windows Forms App solution with the main form

Added a Net Core Class Library project to use as a multiuse library

I'd like to add a project with the sole purpose of creating a UserControl that can be used by multiple forms.

When I do an Add New Project I can't find anything to create a UserControl.
 
Last edited:
Solution
WinForms support in .NET 5.0 is still a bit rough around the edges. There's no Windows Control Library project template like there is for .NET Framework. I just created a C# Class Library (.NET Core) project and then edited the project file from this:
XML:
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

</Project>
to this:
XML:
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net5.0-windows</TargetFramework>
    <UseWindowsForms>true</UseWindowsForms>
  </PropertyGroup>

</Project>
After building, I was then able to right-click the project in the Solution Explorer and add a user control.
You can add a new project that is a library type. Within that project, you can then add a user control.
 
WinForms support in .NET 5.0 is still a bit rough around the edges. There's no Windows Control Library project template like there is for .NET Framework. I just created a C# Class Library (.NET Core) project and then edited the project file from this:
XML:
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

</Project>
to this:
XML:
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net5.0-windows</TargetFramework>
    <UseWindowsForms>true</UseWindowsForms>
  </PropertyGroup>

</Project>
After building, I was then able to right-click the project in the Solution Explorer and add a user control.
 
Solution
WinForms support in .NET 5.0 is still a bit rough around the edges. There's no Windows Control Library project template like there is for .NET Framework. I just created a C# Class Library (.NET Core) project and then edited the project file from this:
XML:
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

</Project>
to this:
XML:
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net5.0-windows</TargetFramework>
    <UseWindowsForms>true</UseWindowsForms>
  </PropertyGroup>

</Project>
After building, I was then able to right-click the project in the Solution Explorer and add a user control.



What does <UseWindowsForms>true</UseWindowsForms> do.

Trying Net 5 I could not get System.Windows.Forms to register correctly.

Tried adding that and it seems to fix it. Why?
 
Look at the project in the Solution Explorer thoroughly before and after and you'll see what the difference is.

'Adding <UseWindowsForms>true</UseWindowsForms> to the <PropertyGroup> added the dependencies
'C:\Program Files\dotnet\packs\Microsoft.WindowsDesktop.App.Ref\5.0.0\

I looked in that folder. Is that something special for Net 5 or has that always been there.

Just curious. What is it?

Is it something that I might not need after Net 5 get more mature?

Can I add a comment to the project file that the system will ignore?
 
It's the directive that tells the IDE to include the WinForms package for .NET 5. I expect that you'll always need it, but the hope would be that they add a project template that does it for you, just as they have with the WinForms application project. I worked out that you could get a class library to work by examining an application project and seeing what was different, then experimenting.

By the way, you can create your own project templates if you want to. I haven't done it for a long time so can't remember the details, which may have changed since I last did it anyway. Basically, you create a project from an existing template, make the changes you want and then export it as a template. If this is a project type you might need again, maybe you should research how exactly to do that.
 
MSB3290 Failed to create the wrapper assembly for type library "{215d64d2-031c-33c7-96e3-61794cd1ee61}". Type library 'System_Windows_Forms' was exported from a CLR assembly and cannot be re-imported as a CLR assembly. LibraryNative0 C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets

I'm converting a class library and cleaned up all errors except the above, which I can find no reference to. Can you tell me the kind of thing I should look for in my code.
 
The obvious first step is to search for the error code and find out what it means and what existing solutions there may be.
Naturally, I did, and only got some general suggestion to use community.

I'll try again but I already di a few times.

Edit: I really did not search - I clicked the error message in the error list and thought the was equivalent. Guess not.

If that's the problem, how can I possibly get around it. I need 'tom"

I'll try again to add a reference and see if that helps'

It did not work the first time because I selected the reference rather than checking it - so it really did not get added.

Thanks for the quick reply.
 
Last edited:
Back
Top Bottom