What is the best way to copy or cut/paste a form from one project to another project within a Solution in Visual Studio?
Whenever I try it, using the solution explorer (drag and drop or right clicking cut and paste), it only copies the underlying C# code and not the necessary 'bits and pieces' that help you visualise the form via the form designer.
Select Visual Studio menu Project -> Add Existing Item... Then select (browse) in the file dialog the Form. cs (Form = name of the form) you want to add to the project and the form (with all associated files) is (copied and) added to your project.
Right click on your project file in Solution Explorer and choose Add -> Existing Item. Show activity on this post. You need to copy all the files. You should even be able to open two instances of Visual Studio and drag and drop the form from one to the other.
.cs
, .designer
, resx
to the target solution folder.Add existing item
and add the designer file first. .cs
file should come in as well. .cs
file.resx
file using Add existing item
.To copy a form from one VS2013 project to another, the easiest, (fewest mouse clicks) is:
In your target project, right mouse click on your parent folder (likely your project) and in the menu select, "Add; Existing Item".
Then select the form's cs file in your source project. For example select Form.cs (not the Form.Designer.cs or Form.resx).
Open Form.cs in your target project and change the namespace (all instances).
Close and reopen Form.cs and you will see all the controls, etc.
Figured it out - knew it would be something daft.
Apparently the target project should have the references:
included within the project FIRST before you do any copy or pasting or else you will get the problem I described.
Thanks for anyone who tried to help BTW.
It's way easier now in 2012. Just go to File > Add > Existing Project > Drag and drop the form into the project that you want to add it to.
I tried the following process in Visual Studio 2012 and it worked.
Make sure that you copy not only the Form.cs, but also the Form.designer.cs and Form.resx files.
But I need to ask why you would want to maintain a copy of the same form in multiple projects within the same solution. Sounds as if you might want to generalize the code in it, put it in a class library and re-use it in the places where it is applicable.
Use ReSharper (get the demo), right-click the class in Code View, Refactor->Move, and move it to another project.
The added benefit is that if you change the namespace by moving it between projects, it'll auto-update all references.
If you want to forms in same machine then simply you can do this.
In Solution Explorer Project --> Add --> Add Existing Item.
Select only .cs (Sample.cs)
file from source directory and Refresh the current solution Explorer it will Work.
I finally figured out as followings:
1) copy 3 files of your wished form whose file extension are .cs
, .resx
, .Designer.cs
.
2) Add Existing and select formName.cs
.
3) check the namespace and Name property value of the added form.
4) In Program.cs
file, use using NameSpace;
and replace new NameSpace.formName()
in the line of Application.Run()
.
For Visual Studio V.12 this is the only method which worked for me (It is a mix of above entries):
I am using Visual Studio 2010, below are the steps I performed:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With