Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • 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.

like image 414
Ian Boyd Avatar asked Nov 21 '11 22:11

Ian Boyd


People also ask

How do you change the namespace of a project in Visual Studio?

You can change the default namespace: -> Project -> XXX Properties... Instead of Find/Replace, you can also right click the namespace in code and Refactor->Rename.

Can we rename namespace?

Assigned Tags. you can't rename a namespace. if it's only one object you can copy and rename it.

What is the default namespace in C#?

The root namespace is also the default namespace for C# programs.

What is namespace in Visual Studio?

Visual Studio assigns your project name as the default root namespace for all code in your project. For example, if your project is named Payroll , its programming elements belong to namespace Payroll . If you declare Namespace funding , the full name of that namespace is Payroll. funding .


2 Answers

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

like image 107
brpyne Avatar answered Oct 31 '22 10:10

brpyne


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

like image 40
georgiaboy82 Avatar answered Oct 31 '22 11:10

georgiaboy82