question

mxdog-8081 avatar image
0 Votes"
mxdog-8081 asked JackJJun-MSFT commented

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

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-formsdotnet-standard
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@mxdog-8081, is any update?

0 Votes 0 ·

1 Answer

reza-aghaei avatar image
0 Votes"
reza-aghaei answered reza-aghaei edited

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



image.png (41.4 KiB)
image.png (75.1 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.