How to get a working Control in VS-2022 for a net6 app C#

mx dog 1 Reputation point
2022-07-30T06:59:37.53+00:00

I have been trying to build a custom control for one of my apps all night and always end up with a greyed-out control in my tool box.

I have tried various flavors of windows forms apps net, desktop with different forms of control libraries all from templates in VS-2022 . My question is WHAT template or what do I have to do to get a working control for an application with the following Project properties . This app targets net 6.0 but all the control templates seem to target net framework 4.X. MS has thrown out so many frameworks lately I have a hard time keeping track of what works with what anymore or even what the differences are .. like what is different from net6 then net framework 4.x and why are there so many flavors all called net ? anyway ...

Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows10.0.22621.0</TargetFramework>
<RootNamespace>OCR</RootNamespace>
<Nullable>enable</Nullable>
<UseWindowsForms>true</UseWindowsForms>
<ImplicitUsings>enable</ImplicitUsings>
<StartupObject>OCR_Fromgame.Program</StartupObject>
<SupportedOSPlatformVersion>10.0.22621.0</SupportedOSPlatformVersion>
<ApplicationIcon>ext\Icon.ico</ApplicationIcon>
</PropertyGroup>

I am sure the confusion is all mine ..but some direction what exactly i need to build a user/custom control for this app would be great ..

The actual control is so simple it is a picture box 10x10 with an X in it and a label to mark screen coordinates on a form ..wasted a whole day here trying to get it into the tool box ..

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,833 questions
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,395 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Reza Aghaei 4,936 Reputation points MVP
    2022-07-30T08:39:58.77+00:00

    You don't necessarily need to put the custom control in another library, unless you want to use it in multiple projects. If it's just one Windows Forms project, you can easily add different types of control to the project (including new User Control or Custom control or any class which is deriving from a Control) and after a build if you open a Form in design mode, the control appears in ToolBox.

    If you are looking for creating a Windows Forms library to have some controls which you can reference in other project, read the rest of the answer.

    Windows Forms Control/Class Library - .NET 6

    You are looking for either of the following, targeting .NET (not .NET Framework):

    • Windows Forms Class Library
    • Windows Forms Control Library

    Both project templates will create the same project type:

    <Project Sdk="Microsoft.NET.Sdk">  
      
      <PropertyGroup>  
        <TargetFramework>net6.0-windows</TargetFramework>  
        <Nullable>enable</Nullable>  
        <UseWindowsForms>true</UseWindowsForms>  
        <ImplicitUsings>enable</ImplicitUsings>  
      </PropertyGroup>  
      
    </Project>  
    

    The only difference is the Control Library project contains a UserControl1, but the Class Library contains Class1.

    226374-image.png

    Example - Creating and using a Windows Forms Control/Class Library - .NET 6

    1. Create a new project or type Windows Forms (not .NET Framework) and choose .NET 6 as the framework, choose WinFormsApp1 as the name of the project
    2. Create a new project or type Windows Forms Control Library (not .NET Framework) and choose .NET 6 as the framework, choose WinFormsControlLibrary1 as the name of the project
    3. Right click on Dependencies of WinFormsApp1, and click on Add Project Reference ... and check WinFormsControlLibrary1 as the reference.
    4. Close all designers and Rebuild the solution.
    5. Double click on Form1 in WinFormsApp1 to open it in designer.
    6. Open toolbox, and drop an instance of UserControl1 on the form.

    226340-image.png


  2. Amin darestani 0 Reputation points
    2023-01-26T05:54:08.7166667+00:00

    it works but what about when we want to use the object dll file in the toolbox items in another separate project?

    it makes an error in the tool box when adding this dll.

    0 comments No comments