This is why you should avoid partial
classes, unless you are writing code generators...
The problem is that you need to make the Form1Part2.cs
dependent on Form1.cs
, and the only way to do that is to edit the project file:
<ItemGroup>
<Compile Include="Form1.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Form1.Designer.cs">
<DependentUpon>Form1.cs</DependentUpon>
</Compile>
<Compile Include="Form1Part2.cs">
<DependentUpon>Form1.cs</DependentUpon>
</Compile>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
<SubType>Designer</SubType>
</EmbeddedResource>
<Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
</None>
<Compile Include="Properties\Settings.Designer.cs">
<AutoGen>True</AutoGen>
<DependentUpon>Settings.settings</DependentUpon>
<DesignTimeSharedInput>True</DesignTimeSharedInput>
</Compile>
</ItemGroup>
The "magic" part is this one:
<Compile Include="Form1Part2.cs">
<DependentUpon>Form1.cs</DependentUpon>
</Compile>
Because when you add the partial
to your code and rename the class it contains, Visual Studio helpfully calls it a form subtype, like this:
<Compile Include="Form1Part2.cs">
<SubType>Form</SubType>
</Compile>
Which causes the designer to be loaded whenever you click on it, and because it doesn't have a dependency or a .designer.cs
file, the Forms Designer doesn't load up the actual form you are designing, just a blank designer (and if you add controls to it, it appears in the partial class and creates an additional RESX file causing all kinds of issues).
Change that XML to make your Form1Part2.cs a dependency of Form1.cs
. You can only do this by manually editing the .csproj file. Easiest way is to open it in notepad, but you can edit it in Visual Studio by right clicking on it, unloading the project, then right clicking again and select "Edit xxx.csproj". Make the changes then right click the project and reload it.
After completing this, your new .cs file will appear under the Form1.cs along with the designer and code-behind:
