MarisP
MarisP

Reputation: 987

Tried to rename form and now I can't edit it

I tried to rename my form, I did that, but then.. I don't even know how to explain it. The form is still visible in the solution explorer section, but I can't open it. It says that "The item 'Form4.cs does not exist in the project directory. It may have been moved, renamed or deleted."

What to do?

Upvotes: 0

Views: 2016

Answers (1)

BartoszKP
BartoszKP

Reputation: 35901

Form names aren't something magically hidden somewhere so it should be easy to clean up this mess. If you open your .csproj file, you should see a section that contains something similar to this:

<Compile Include="Form1.cs">
  <SubType>Form</SubType>
</Compile>
<Compile Include="Form1.Designer.cs">
  <DependentUpon>Form1.cs</DependentUpon>
</Compile>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<EmbeddedResource Include="Form1.resx">
  <DependentUpon>Form1.cs</DependentUpon>
</EmbeddedResource>

As you may guess, these all entries (except "AssemblyInfo.cs" and "Program.cs") should relate to existing files in the file system, and usually their names should match each other (i.e. XXX.Designer.cs corresponds to XXX.cs and XXX.resx).

Additionally, in a simple project these are the common places where the form name is used throughout the code:

...\Form1.cs(13):    public partial class Form1 : Form
...\Form1.cs(15):        public Form1()
...\Form1.Designer.cs(3):    partial class Form1
...\Form1.Designer.cs(43):            // Form1
...\Form1.Designer.cs(49):            this.Name = "Form1";
...\Form1.Designer.cs(50):            this.Text = "Form1";
...\Program.cs(23):            Application.Run(new Form1());

If you'll manage to fix all these places and make them coherent everything should be fine.

You can either reverse whatever changes you've made in the file system for the files to match entries listed above, or you can close VS and edit the .csproj file manually, to make it corresponding with the files you have.

Usually you should rename forms and classes using VS interface, as it will ensure that all changes are propagated. So perhaps in your case it is easier to revert your manual change, and let VS do this for you properly.

Upvotes: 2

Related Questions