I'm using Visual Studio 2022, trying to migrate some components and user controls from NET Framework 4.8 that can be added to the Windows Forms designer, to the Windows Forms Designer of a NET Core 6 project, but I've run into an obstacle that I have no idea how to overcome.
I'll simplify my problem with the following example:
I have a class library project built on NET Core 6.0 targeting Windows OS.
This is the .csproj file content:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0-windows</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>
</Project>
And this is the only class contained in my project:
using System;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Runtime.Versioning;
namespace MyCustomControls {
[DisplayName(nameof(MyCustomControl))]
[Description("My Custom Control.")]
[DesignTimeVisible(true)]
[ToolboxItem(true)]
[DesignerCategory(nameof(UserControl))]
[ToolboxBitmap(typeof(Control), "Control.bmp")]
[ComVisible(true)]
[SupportedOSPlatform("windows")]
public class MyCustomControl : UserControl {
public MyCustomControl() {}
}
}
Now, I created a Windows Forms app under NET Core 6.0 that is also targeting Windows.
This is the .csproj file content:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>
</Project>
The problem is that when I drag and drop to the controls toolbox the file.dll containing the sample user control, it is not added.
Nothing happens, no warning, no error, nothing of nothing, Visual Studio it just doesn't add my custom user control to the controls toolbox. And I can't find any info about why is this happening and/or the steps to properly add a custom user control in the controls toolbox when working with NET Core.
I've ensured that the AutoToolboxPopulate option in Visual Studio's options is enabled at Options -> General -> Windows Forms Designer
.
In my NET Core's Windows Forms project, if I create a new class file and I paste the code of my user control, after compilation the control is automatically added to the control toolbox:
(of course only for that project)
So I'm starting to think that maybe I'm not missing to add a required class attribute on the type definition of my user control class, and maybe the real problem could be that .NET Core lacks of support to import controls from a "external" DLL file, but I insist about I can't find any official documentation or article about adding custom user controls to the Windows Forms Designer of a NET Core solution, so I can't say for sure.
I don't think that my Visual Studio installation is broken, I think It is just a problem of inexperience with .NET Core from my side, so probably I'm not aware about how to do this in the correct way to display a user control in the controls toolbox of a Windows forms designer on NET Core, but anyways I tried this suggestion just in case:
And after resetting the toolbox, closing Visual Studio and deleting the related toolbox cache files (in my case I only found a file with name: ToolboxItemDiscoveryCache.{long id here}
), it does not solve the problem.
That suggestion is listed with some other suggestions in this question:
...none of those suggestions worked for me either, although the problem and scenery of that user it differs significantly from mine.
I found what I think might be evidence that the ability to import/populate user controls from a NET Core DLL file, to the NET Core Windows Forms designer it is a feature that is currently not supported:
@ Klaus Loeffelmann [MSFT] | Aug 30, 2023 :
Currently, WinForms for .NET (5, 6, 7, 8) only supports Toolbox deployment of Controls, which are either part of the Solution or imported as a NuGet package. We are aiming for Visual Studio 2022 17.8 to have DLL-Reference Support for Controls/Toolbox Deployment. Until then, please use best practice, which is packing your Control Library as a NuGet Package which you then import in the respective WinForms projects.
Full discussion:
...this problem it seems to be known since year 2021, so maybe this support will never arrive to Visual Studio.
And since year 2021 they haven’t even had the ability to add a simple warning message saying something like: “this feature is not yet supported” when trying to import controls from a DLL to prevent affected users like me from wasting hours and hours trying to understand what the hell is happening, trying things and researching on the Internet. I find it incredible from the side of Visual Studio developer team. Just incredible.
The workaround (but not a real solution or fix to the main problem) is to pack the NET CORE assembly containing the user controls as a nuget package, then use the nuget package console to install the package.
After installation, it seems that the nuget package is always automatically loaded for every newly created NET Core Windows Forms project, and the controls are automatically added at the (top of the) controls toolbox, so not that bad at all.
Note: this workaround, when "adding" the controls to the controls toolbox through a nuget package, it has the aesthetic disadvantage that it does not show custom icons (if specified through the ToolboxBitmap
attribute class) for the controls.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With