简体   繁体   中英

Copy User Control from one project to another within same solution

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM