What is the correct way to change the namespace of a WinForm 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 must be in the Bajingo namespace. What is the correct way to change the namespace of a form?

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

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

makes the designer break down. I can edit the MyForm.Designer.cs code MyForm.Designer.cs manually:

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

But I am told by Visual Studio ( and SO users who suggest "use refactoring" ) so as not to manually edit the constructor file.


Mandatory filler material:

  • I checked the properties window for the form, and there is no Namespace property:

    enter image description here

  • I tried just renaming it, and let Visual Studio refactoring hit:

    enter image description here

    Except that Visual Studio wants to rename the namespace everywhere, in everything. I just want this form.

+9
source share
3 answers

You can edit the constructor file. Stay away from the region with the inscription "Windows Form Designer generated code" and everything will be fine.

+15
source

Starting in VS 2017, you can select the namespace you want to rename, right-click on it, click " Rename , then change it to the new namespace name, then click" Apply in the dialog that should appear when you click " Rename

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

Enter a new namespace name, make sure the Preview Changes option is Preview Changes and click Apply :

2) step 2

Check all the places you want to change:

3) enter image description here

0
source

You need to click on the Show All Files icon at the top of Solution Explorer before renaming the form. If you do not, some support files will not be renamed.

0
source

Source: https://habr.com/ru/post/902063/


All Articles