简体   繁体   中英

C# Form1.cs no longer showing in Solution Explorer

I am new to programming and am working on a C# project and my form1.cs is no longer in my solution explorer. It does however show in class view. I have tried right clicking and adding existing item but this doesn't work. Is it possible to get the form1.cs back from class view into solution explorer? Thanks.

Try to click "Show all items" button on top of Solution Explorer, it will show all files that not in your project. And if your Form.cs will appear then right click on it and select "Include in project" menu.

It may be that the form has got excluded from your project. At the top of your Solution Explorer, click "Show All Items".

在此输入图像描述

If the form then appears, right click it and select "Include In Project".

UPDATE From what has been said, it sounds like the Form.cs file may have got "disconnected" from the designer and resx files. Close Visual Studio and open the .csproj file in an XML editor or just Notepad and check that you have something resembling the following in an "ItemGroup" element.

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

Make any changes required and then re-open your solution.

I had the same Problem and was struggling with it quite a while. I solved it by installing the desired target framework (in my case .NET 4.5.1 ) manually (from offline installer) AND the .NETCoreApp components (from "modify" in the VS installer).

Then I created a new solution with a console project in it and set the target framework to .NET 4.5.1.

When I now create a new Forms project, everything is back to normal (it uses 4.5.1 by default) and the Forms.cs file shows up in solution explorer.

I will try if I can fix my broken projects that way too...

In my .csproj file there was no

<EmbeddedResource Include="Form1.resx">
      <DependentUpon>Form1.cs</DependentUpon>
</EmbeddedResource>

Not before and not after the Form.cs was shown.

Right-click and select Exclude From Project then Right-click and select Include In Project the same model, And you will see the form

I can't upvote Salem's answer, so I'm here to confirm his answer worked for me. Visual studio 2019:

  • Right click the form, choose Exclude from Project
  • Show All file in project
  • Right click the form, choose Include in project
  • Turn off Show All file in project
  • Wait 10-15 seconds before check the form

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM