Questions › Copy User Control from one project to another within same solution
McRealz Asked

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.

Comments :
MethodMan replied

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..?

Muckeypuck replied

instead of copying and pasting them in repeatedly like that, why don't you make a control library project and reference that?

McRealz replied

@MethodMan This is a Windows Application.

McRealz replied

@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.

Muckeypuck replied

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?


0 Answers :