Copy User Control from one project to another within same solution
Asked Answered
P

1

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.

Presidio answered 27/1, 2016 at 18:4 Comment(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..?Impenetrability
instead of copying and pasting them in repeatedly like that, why don't you make a control library project and reference that?Harkey
@Impenetrability This is a Windows Application.Presidio
@Harkey 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.Presidio
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?Harkey
@Harkey Yes, there is only one copy+paste going on from project 1 and 2. So the namespace edits in step 2 are referring to the control's .cs and .designer.cs files. Step 3 is referring to the .designer.cs file on any form in which that control was located on. The projects where the control is being copy+pasted from are used across multiple projects, is that what you mean by your question?Presidio
yep. thats what i was asking. to be honest, i don't have an answer. I always do a replace all on the solution in those situationsHarkey
S'all good, it is odd how those namespaces dont get changed is all. Or there isn't a "move control" command that handles this. How do you mean a "replace all"?Presidio
M
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.

Mew answered 17/11, 2017 at 14:11 Comment(2)
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?Stonewort
this is ridiculous but works if following every step EXACTLYLeprose

© 2022 - 2024 — McMap. All rights reserved.