Resolved - Create Net 5 UserControl (2024)

  • Visual Studio .NET
  • VS.NET General Discussion
  • Thread starteraaaron123
  • Start dateJan 14, 2021

A

aaaron123

Member
Joined
Jan 3, 2021
Messages
18
Programming Experience
1-3
  • Jan 14, 2021
  • #1

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

J

  • J

    jmcilhinney

  • Jan 14, 2021

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.

Sort by dateSort by votes

Skydiver

Staff member

Joined
Apr 6, 2019
Messages
8,067
Location
Chesapeake, VA
Programming Experience
10+
  • Jan 14, 2021
  • #2

You can add a new project that is a library type. Within that project, you can then add a user control.

Upvote0Downvote

J

jmcilhinney

C# Forum Moderator

Staff member

Joined
Apr 23, 2011
Messages
5,349
Location
Sydney, Australia
Programming Experience
10+
  • Jan 14, 2021
  • #3

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.

Upvote1Downvote

Solution

aaaron123

Member
Joined
Jan 3, 2021
Messages
18
Programming Experience
1-3
  • Jan 14, 2021
  • #4

Skydiver said:

You can add a new project that is a library type. Within that project, you can then add a user control.

I know it has to be simple but I added a class library and then tried add new item to it but could not find the correct item to add.

Upvote0Downvote

A

aaaron123

Member
Joined
Jan 3, 2021
Messages
18
Programming Experience
1-3
  • Jan 15, 2021
  • #5

jmcilhinney said:

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?

Upvote0Downvote

J

jmcilhinney

C# Forum Moderator

Staff member

Joined
Apr 23, 2011
Messages
5,349
Location
Sydney, Australia
Programming Experience
10+
  • Jan 15, 2021
  • #6

Look at the project in the Solution Explorer thoroughly before and after and you'll see what the difference is.

Upvote0Downvote

A

aaaron123

Member
Joined
Jan 3, 2021
Messages
18
Programming Experience
1-3
  • Jan 15, 2021
  • #7

jmcilhinney said:

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?

Upvote0Downvote

J

jmcilhinney

C# Forum Moderator

Staff member

Joined
Apr 23, 2011
Messages
5,349
Location
Sydney, Australia
Programming Experience
10+
  • Jan 15, 2021
  • #8

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.

Upvote0Downvote

A

aaaron123

Member
Joined
Jan 3, 2021
Messages
18
Programming Experience
1-3
  • Jan 16, 2021
  • #9

jmcilhinney said:

WinForms package for .NET 5. I expect that you'll always need itt.

Good, I don't need to make a note to check this ocassionally.

Thanks - to bad there can only be one reply marked as solution.

Upvote0Downvote

A

aaaron123

Member
Joined
Jan 3, 2021
Messages
18
Programming Experience
1-3
  • Jan 16, 2021
  • #10

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.

Upvote0Downvote

J

jmcilhinney

C# Forum Moderator

Staff member

Joined
Apr 23, 2011
Messages
5,349
Location
Sydney, Australia
Programming Experience
10+
  • Jan 16, 2021
  • #11

The obvious first step is to search for the error code and find out what it means and what existing solutions there may be.

Upvote0Downvote

A

aaaron123

Member
Joined
Jan 3, 2021
Messages
18
Programming Experience
1-3
  • Jan 16, 2021
  • #12

jmcilhinney said:

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:

Upvote0Downvote

Similar threads

  • Question

QuestionDelay in inserting images in UserControl displayed in TabItem

  • Elad Yehuda
  • Mar 5, 2024
  • Windows Presentation Foundation (WPF)
Replies
1
Views
549

Mar 5, 2024

Skydiver

  • Question

How can I remove a git and github repository connection to a solution in Visual Studio 2022?

  • complete
  • Jul 18, 2024
  • Third Party Products
Replies
1
Views
171

Jul 18, 2024

Skydiver

J

  • Question

QuestionIs this the best way to implement multi-lingual into a large application?

  • jackrabbit
  • Jul 8, 2024
  • Localization and i18n (Internationalization)
Replies
3
Views
470

Jul 9, 2024

Skydiver

P

  • Question

QuestionSet up MudBlazor for Blazor Wep App in .Net 8

  • Pablo
  • Jul 6, 2024
  • ASP.NET Core, Razor, and Blazor
Replies
2
Views
206

Jul 6, 2024

Skydiver

  • Question

Windows Form project Deployment with WebView2

  • philip
  • Mar 27, 2024
  • Deployment
Replies
4
Views
523

Mar 31, 2024

philip

Share this page

Latest posts

  • How to open web form web sites

    • Latest: SaeedP

    Web Forms

  • Read last 6 bytes

    • Latest: Skydiver

    C# General Discussion

  • P

    WNetAddConnection2 failure exit

    • Latest: patrick

    C# General Discussion

  • P

    TaskDefinition Run whether user is logged on or not C#

    • Latest: patrick

    C# General Discussion

  • Resolveddraw string

    • Latest: Skydiver

    Windows Forms

  • Visual Studio .NET
  • VS.NET General Discussion
Resolved - Create Net 5 UserControl (2024)

FAQs

How to create a user control in C#? ›

Add a user control to a project

Right-click on the project and choose Add > User Control (Windows Forms). In the Name box, type a name for your user control. Visual Studio provides a default and unique name that you may use.

What is an example of a user control? ›

Most digital interfaces provide undo and redo options. This feature is a prime example of user control—and it lets users easily reverse their actions. For instance, text editors often have undo and redo buttons that let users revert their changes or reapply them—respectively.

How do I add custom controls to my toolbox in VB net? ›

To create a new tab for the custom controls, right-click anywhere in the Toolbox window, select Add Tab, and then provide a name for the new tab. Then, right-click anywhere inside the new tab, and then select Choose Items. The Choose Toolbox Items dialog box will be displayed.

What is the difference between user control and custom control in C#? ›

The main difference between them- User Control is a page file with extension . ascx which can only be used within a single application or project But custom controls are assemblies(dll files) that can be used in multiple applications.

How to create a controller in C#? ›

Creating a Controller Class

Right-click the Controllers folder and select the menu option Add, New Item and select the Class template (see Figure 4). Name the new class PersonController. cs and click the Add button. Modify the resulting class file so that the class inherits from the base System.

Why do we use user control in C#? ›

If you want some changes in your code then you have to write code at one place (user control) that effect in every web form or every Form of window application. Using this technique you can save your extra effort and also save your time. Here I have to show the Demo make user control in window application C#.

How to create custom control in C#? ›

How to Create a Custom Control
  1. Start a new Windows Forms Control Library project; see:
  2. Remove UserControl1.cs which has been created by default and add a new class; see:
  3. Add the following namespaces to the new class (in our case its ExtdTextBox.cs) and inherit the framework's TextBox control class:
Mar 16, 2015

How do I add UserControl to Toolbox? ›

4 Answers
  1. If your UserControl is in a library you can add this to you Toolbox using. Toolbox -> right click -> Choose Items -> Browse. ...
  2. If the UserControl is part of your project you only need to build the entire solution. After that, your UserControl should appear in the toolbox.
Jan 19, 2012

What is the difference between user control and Web page? ›

A user controls differs from an ASP.NET Web page in these ways: The file name extension for the user control is . ascx. Instead of an @ Page directive, the user control contains an @ Control directive that defines configuration and other properties.

How to create ASP.NET control? ›

You need to use a placeholder control, which is a standard ASP.Net control for adding controls dynamically at run-time. You place the placeholder control at the location in your page markup where you would like the dynamically created button to appear. Then, just use the line of code placeHolder1. Controls.

What is the difference between user control and server control? ›

A User Control is a partial web page, created in the same way as any other web page in ASP.NET, except that it has an . ASCX extension, and it can be embedded in your other ASPX pages. Server controls are controls that execute on the server and render markup to the browser.

How to create custom control in asp net MVC? ›

Create a new website. Right click the solution (not the project) at the top of the tree in the Solution Explorer. In the New Project dialog box, select ASP.NET Server Control from the project templates. The above step adds a new project and creates a complete custom control to the solution, called ServerControl1.

What are visual basic controls? ›

Visual basic controls are the columns that aid in creating GUI Based Applications in VB.Net quickly as well as easily. These are things that you can drag to the Type using the Control tool kit in the IDE.

How do I add custom controls to control center? ›

You can customize Control Center by adding more controls and shortcuts to many apps, such as Calculator, Notes, Voice Memos, and more.
  1. Go to Settings > Control Center.
  2. To add or remove controls, tap or. next to a control.
  3. To rearrange controls, touch. next to a control, then drag it to a new position.

How to control user input in C#? ›

C# Input. In C#, the simplest method to get input from the user is by using the ReadLine() method of the Console class. However, Read() and ReadKey() are also available for getting input from the user. They are also included in Console class.

How to call a user control in asp net C#? ›

To use a User Control in an . aspx we need to register it using the Register page directive, the register page directive has the following properties: <%@ Register Src="~/student. ascx" TagPrefix="uc" TagName="Student" %>

Which method is used to put the user control on the form in C#? ›

Test the UserControl
  1. Create a new Visual C# project. To do this, follow these steps: ...
  2. Add the UserControl1 control to the toolbox. On the Tools menu, click Choose Toolbox Items. ...
  3. Drag UserControl1 from the toolbox (under Windows Forms) to Form1. cs.
  4. Drag a Button control from the toolbox to UserControl1.
May 7, 2022

References

Top Articles
Latest Posts
Article information

Author: Neely Ledner

Last Updated:

Views: 6589

Rating: 4.1 / 5 (42 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Neely Ledner

Birthday: 1998-06-09

Address: 443 Barrows Terrace, New Jodyberg, CO 57462-5329

Phone: +2433516856029

Job: Central Legal Facilitator

Hobby: Backpacking, Jogging, Magic, Driving, Macrame, Embroidery, Foraging

Introduction: My name is Neely Ledner, I am a bright, determined, beautiful, adventurous, adventurous, spotless, calm person who loves writing and wants to share my knowledge and understanding with you.