16

I have a form in my existing project.

My current task is to make a duplicate of an existing form and change few things on the new form. Making a copy of the form cs files would not do since the existing contents themselves refer to file information.

Simply put, I am trying to crate a form name MyNewForm, which will be a direct duplicate of MyCurrentForm without causing any naming conflict that may arise in mere copy pasting of code content.

What is the fastest way I can achieve this?

Ry-
  • 218,210
  • 55
  • 464
  • 476
Shamim Hafiz - MSFT
  • 21,454
  • 43
  • 116
  • 176
  • There are actually several files associated with a form. It would be a drag and erroneous task to edit those files. – Shamim Hafiz - MSFT Nov 01 '10 at 13:03
  • Normally a rename of a file in VS will also rename the associated files (.designer etc), in particular if they are the type that gets generated by VS to begin with. – Oded Nov 01 '10 at 13:04

4 Answers4

30

Copy the form in visual studio solution explorer. Rename it. And change the class name manually both in .cs and .Designer.cs files. Do not use VS refactoring feature as it blows away references to the original class.

Kamyar
  • 18,639
  • 9
  • 97
  • 171
  • How to make this copy in Visual Studio Solution Explorer? I tried copy pasting, but the old file name is replaced and there is some kind of conflict. – Shamim Hafiz - MSFT Nov 01 '10 at 13:51
  • @Gunner: I don't see any conflict. I just right click->Copy and right click on the appropriate folder->Paste. If the files are in the same folder, It creates "Copy Of Form1" with all the related file names renamed appropriately. – Kamyar Nov 01 '10 at 13:57
  • Thanks. This has worked for me. I overlooked the newly created file. – Shamim Hafiz - MSFT Nov 01 '10 at 14:28
  • 1
    I would also suggest renaming all references to the former class in the .cs and .Designer.cs files. I think a manual find-replace is better than refactor as refactor may blow away references to your original class. – gonzobrains Nov 09 '12 at 00:44
  • @gonzobrains I actually does that. You must rename references manually. – Kamyar Nov 09 '12 at 10:13
12

To duplicate a form (in the same project):

  1. Right click on the source form --> Copy
  2. Right click on the destination folder/project --> Paste
  3. Right click on the new form --> Rename
  4. Change manually the class name in .cs
  5. Change manually the constructor name in .cs
  6. Change manually the class name in .Designer.cs

Enjoy!

David
  • 37,109
  • 32
  • 120
  • 141
1

Why do you need to make a duplication of the form? Try to find some refactoring that can help you, e.g. create some base form and extract common logic there.
Every time you make a duplication kitten dies!

Andrew Bezzub
  • 15,744
  • 7
  • 51
  • 73
0

You can just add a new blank form and then select all items on the original aform and paste them onto the new form. This will not copy the code behind though. But that can also be solved with copy paste.

This will not cause any renaming conflicts.

Liviu Mandras
  • 6,540
  • 2
  • 41
  • 65