5

Occasionally, a C# project will have a user control in it that belongs to another C# project and I will have to move it over. Attempting to simply copy + paste that user control over results in namespace errors. Let's say project 1 has a namespace of namespace General.Category1.Controls and project 2 has a namespace of namespace General.Category2.Controls.

The steps I initially took were:

1) Copy + paste control (which includes .cs, designer.cs and .resx files) from project 1 to project 2

2) Change namespace to project 2's namespace General.Category2.Controls

At this point upon building, I received additional errors from the forms on which this control is used before the copy + paste that states 'The type of namespace name 'UserControl1' does not exist in namespace 'General.Category1.Controls' (are you missing an assembly reference?). It turns out that the designer.cs form files did not update to the correct namespace after the copy + paste.

So then I add to add an additional step:

3) Change declarations to match new namespace of General.Category2.controls aka private General.Category2.Controls UserControl userControl1;

I'm confused why the designer.cs files wouldn't have updated that since it's not usually advised to edit designer.cs files. But then again, maybe it's ok to modify them as long as you don't touch the InitializeComponent() method as it states in the actual file itself.

In conclusion, why do all of the designer.cs not update automatically? Is the actual answer to know where this control is used and update the declarations myself as I have done?

[EDIT]: The first 2 steps are for the control itself (it's .cs and .designer.cs files). The 3rd step is for associated with any forms in which the control was placed on before copying it over because it is still using the old namespace, so all of the form .designer.cs files must be changed as well.

8
  • how are you referencing the user controls is this a web app or windows app..? in it's a web app can you show how you are referencing it in the .aspx file..?
    – MethodMan
    Commented Jan 27, 2016 at 18:06
  • instead of copying and pasting them in repeatedly like that, why don't you make a control library project and reference that?
    – Muckeypuck
    Commented Jan 27, 2016 at 18:08
  • @MethodMan This is a Windows Application.
    – mccoyrjm
    Commented Feb 3, 2016 at 15:12
  • @Muckeypuck That's what we're doing, but there are two control library projects. The control was made in the 1st incorrect one, so I am copying + pasting it into the 2nd correct one.
    – mccoyrjm
    Commented Feb 3, 2016 at 15:13
  • something doesn't sound right. if you are pasting it into the second one, you should only have to paste it in once. i guess theres something i dont understand. Are you reproducing your control libraries for every project?
    – Muckeypuck
    Commented Feb 4, 2016 at 2:12

1 Answer 1

20

Been there ! I do not use copy-paste for this. In VS-2017 a UserControl1 can be copied to another project, as follows:

  • In Windows: copy UserControl1.cs, .designer.cs and .resx into your new project directory
  • In VS: right click your project in Solution Explorer, set it as Startup project

  • Right click new project in Solution Explorer, and choose Add Existing Item

  • Now, select only UserControl1.cs file in the file box

  • Wait, Solution Explorer will register the other files, change the symbol
  • In VS, open UserControl1.designer.cs .. change namespace to new project namespace
  • In VS, open UserControl1.cs, change namespace to new project namespace

After this step, rebuild your new project. Go to the form design and open Toolbox. Toolbox will show your UserControl.

Note if you move your control into a new Framework or Core class library, take into account, that WinForms is not referenced yet ! Make sure you add a reference to System.Windows.Forms before adding existing items using my tips above.

2
  • 1
    I did this, and it did show up in my Toolbox, but I cannot open the designer view on the imported control any more. Is there a fix for that? Commented Mar 14, 2019 at 15:46
  • 1
    this is ridiculous but works if following every step EXACTLY
    – 1knueller
    Commented Apr 28, 2022 at 9:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.