Ian Boyd
Ian Boyd

Reputation: 256771

What is the correct way to change the namespace of a WinForm's Form?

i have a form that is currently in the wrong namespace:

MyForm.cs:
namespace DirtyHowdoyoudo
{
   public MyForm()
   {
      ... 

MyForm.Designer.cs:
namespace DirtyHowdoyoudo
{
partial class MainForm
{

The form should be in the namespace Bajingo. What is the proper way to change the namespace of a form?

Note: Editing MyForm.cs to use the new namespace:

MyForm.cs:
namespace Bajingo
{
   public MyForm()
   {
      ... 

causes the designer to break. i could edit the MyForm.Designer.cs code file manually:

MyForm.Designer.cs:
namespace Bajingo
{
partial class MainForm
{

But i am told by Visual Studio (and SO users - who suggest "use refactor") not to manually edit the designer file.


Obligatory filler material:

Upvotes: 19

Views: 14358

Answers (3)

Dave Patrick
Dave Patrick

Reputation: 9

You need to click on "Show All Files" Icon at the top of the Solution Explorer before renaming the form. If you do not then some support files do not get renamed.

Upvotes: 0

georgiaboy82
georgiaboy82

Reputation: 552

As of VS 2017, you can highlight the namespace you want to rename, right click over it, click Rename, then change it to the new namespace name, then click on Apply in the dialog that should have appeared when you clicked Rename.

1) Highlight the namespace you want to rename and right click, then click Rename:step 1

Type the new namespace name, make sure Preview Changes is ticked and click Apply:

2) step 2

Check all the places you want it changed:

3)enter image description here

Upvotes: 5

brpyne
brpyne

Reputation: 1006

You may edit the designer file. Stay clear of the region labeled "Windows Form Designer generated code" and you will be fine.

Upvotes: 19

Related Questions