19

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:

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

    enter image description here

  • i tried to just rename it, and let the Visual Studio refactor kick in:

    enter image description here

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

3 Answers 3

19

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

1
  • I edited the designer files and changed the namespace manually, but still had an issue. Both the designer and resource file were not 'associated' to the project, though I added the form files from another project. I right-clicked on these files and selected 'Include in project', which did the trick.
    – TravelDev
    Jan 3, 2018 at 2:56
5

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

0

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.

1
  • I don't think this answer accomplishes what OP is asking for. "i tried to just rename it, and let the Visual Studio refactor kick in: ... Except that Visual Studio wants to rename the namespace everywhere, in everything. i just want this one form." Jul 9, 2020 at 17:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.