Question

When I write a winforms application, I tend to create an Implementation.cs file for each form.

Say I have Form1.cs, I'd create a new file called Form1.Implementation.cs starting with partial class Form1.

Form1.cs just contains all the event callback methods (what the designer has done), everything else goes to Form1.Implementation.cs. It helps me write more readable code.


I wanted Form1.Implementation.cs to be a "subfile" just like Form1.Designer.cs is, so I edited .csproj file.

<Compile Include="Form1.Implementation.cs">
    <DependentUpon>Form1.cs</DependentUpon>
</Compile>

After reload, however, Visual Studio automatically adds <SubType>Form</SubType> right after DependentUpon element. Doubleclicking Form1.Implementation.cs doesn't show code but a designer with another initial empty form.

It's like

"class Form1, which ISA Form, is(?) multiple forms."

I tried adding DesignerCategory attribute to class Form1, but it affects Form1.cs, as well.

Well, hitting 'Shift-F7' or 'Ctrl-Shift-0' is not a big deal. I wonder if...

  1. it's a glitch of Visual Studio,
  2. the secondary(?) form really exists somehow,
  3. it's going to blow up my winforms project someday
Was it helpful?

Solution

The only way of achieving what you tried is by adding <DependentUpon>, which you already know. Now Visual Studio automatically adds the <SubType>Form</SubType> for any class derived from Form. Since your Form1.cs probably contains the line public partial class Form1 : Form, this is where the SubType is coming from.

The other files - Form1.Designer.cs and Form1.Implementation.cs may contain only partial class Form1, but since partial class definitions across multiple files are still effectively one class definition, Visual Studio detects that it still inherits Form. I believe you may already know that, but just in case here's the MSDN article about the partial keyword. Don't worry about there being multiple instances of Form in this scenario. Remember this still is just one class - Form1, no mater over how many files you spread it.

In the end, all code files containing classes (even partial!) inheriting Form (or UserControl) are automatically opened in the Designer. This behaviour is by design.

The solution here is simple - either make a code file defining a separate class not based on Form, or just use F7 to view the code of that file in Solution Explorer, however annoying that may seem. It doesn't matter if that code file is <DependentUpon> anything or not. Only the inheritance of Form or UserControl matters.

The best solution though, in my opinion, would be to stick to what Visual Studio is giving you:

  • Designer-generated code stays in Form1.Designer.cs
  • Your code (what you put in Implementation), goes in Form1.cs (hit F7 to view that code instead of going to the designer)

This is an approach my team has been sticking to for a few years now. It provides a basic means of separation of Designer code and hand-coded actions. To better separate your code, use a design pattern such as MVP, as suggested by Simon Whitehead in the comments.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top