views:

407

answers:

4

What is the best way to copy or cut/past 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 past), it only copies the underlying C# code and not the necessary 'bits and pieces' that help you visualise the form via the form designer.

A: 

Are you selecting the file, or using the designer to "Copy" all of the controls? Selecting the YourForm.cs file in the Solution Explorer and copying it via copy-and-paste or drag-and-drop to your other project should accomplish what you need.

Adam Robinson
Editied above in line with your question.
Vidar
+1  A: 

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.

Fredrik Mörk
No I just designed a form in the wrong project and now I want to move it into the correct project.
Vidar
I also added the designer.cs and resx files too - still doesnt change anything.
Vidar
OK; I just wanted to raise the flag since you used the word "copy" instead of "move" :o)
Fredrik Mörk
I just tried it (in VS 2008). If I drag the Form.cs file while holding the <SHIFT> key, it will move the form and its associated files to the other project, and behave like a form in that project.
Fredrik Mörk
A: 

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.

Chris Doggett
A: 

Figured it out - knew it would be something daft.

Apparently the target project should have the references:

System.Windows.Forms System.Drawing

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.

Vidar