2

I'm currently facing the issue that my main Form.cs file grows bigger and bigger and I would like to split it into several partial files. These should stick below the main Form (MainForm.cs) Node in the Visual Studio Solution Explorer.

I tried creating the new class file and declared it a partial source file of my MainForm class. However, the newly created partial class file changes it's icon, looking like a form. It instead should stick below the MainForm Node in the Solution Explorer.

To visualize the problem:

Wrongly ordered partial class file

The MainForm.ContextActions.cs file content is as follows:

namespace Interface_Group_Editor
{
    public partial class MainForm
    {
        [...]
    }
}

The content is similar to the MainForm.Designer.cs file. However the .Designer.cs file sticks below the MainForm.cs node while .ContextActions.cs does not!

I only handle absolutely necessary form events in the MainForm.cs file and have my business logic in external classes that I access from the form. However, alone my TreeView code to maintain and edit the form takes about 500 lines of code which could be relocated to a new partial source file. This would help the whole visibility of the Project Structure a lot!

Is there any way to force this behavior to the Solution Explorer?

Vinz
  • 3,030
  • 4
  • 31
  • 52
  • 1
    Have a look at [File Nesting extension](https://visualstudiogallery.msdn.microsoft.com/3ebde8fb-26d8-4374-a0eb-1e4e2665070c). I am using it to combine (manually nest) WPF view + viewmodels (similar to how you want to nest some `cs` inside form group), it's pretty. Not sure if idea of *group nesting* if fine. Another option is to create folder in the project for each big group (containing items and other groups), this however add some stupid namespace issues (perhaps it is also possible to fix), adding folder names to namespace. – Sinatr Jun 20 '16 at 13:05
  • 1
    A related post that actually provides the same answer as Hans Passant below is https://stackoverflow.com/questions/1478610/naming-conventions-for-partial-class-files – Nameless One Jun 20 '16 at 13:50

2 Answers2

7

The IDE does not let you do this, you'll have to edit the .csproj file with a text editor. Notepad will do. Locate:

<Compile Include="MainForm.ContextActions.cs" />

And edit to:

<Compile Include="MainForm.ContextActions.cs">
  <DependentUpon>MainForm.cs</DepedentUpon>
</Compile>

Note how the entry for MainForm.Designer.cs looks like this as well. Do keep in mind that this feature doesn't get tested very often so you may run into some quirks. As far as know it works well, you for example don't get event handlers back when you move them. Removing an event handler is the trickier scenario, I noticed that the MainForm.cs file is marked as modified even though it was never changed. And it won't delete an empty event handler like it normally does. YMMV.

Inheritance is the ultimate power tool in Winforms. You for example can easily derive your own class from TreeView and move all the custom code into that class. It doesn't have to be used more than once. But maybe some day you will :)

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • 1
    I don't want to duplicate your correct answer, but in the past I felt that sub folders were handled in a counter-intuitive way for this. Perhaps you want to add a remark on this, similar to what I said at https://stackoverflow.com/questions/1478610/naming-conventions-for-partial-class-files – Nameless One Jun 20 '16 at 13:55
0

I'm not sure if I really wanted to do this however it works in some fashion for separating the Form code to do some Business logic (or other) code as this post was trying to do. I didn't like that it opened an Empty Form Designer on opening the file.

If you do Hans solution, but change the name of the file to MainForm.SomeExtensionClassYouWant.Designer.cs it no longer opens false Designer on double-clicking the file.

ie: there must be some code on opening when ending with .Designer.cs (or .vb) to open in code rather than the misleading designer.

Tested in VS 2017 in C# and VB.Net

EG:

<Compile Include="Form1.IncludedPartial.Designer.cs">
  <DependentUpon>Form1.cs</DepedentUpon>
</Compile>

Solution View with Partial Form Class under Form

CooPzZ
  • 301
  • 2
  • 12
  • WARNING: Although this is a bit of a hack - Renaming the forms file while loaded in Visual Studio fails miserably with an Error. Probably best to avoid this kind of thing. – CooPzZ Feb 18 '22 at 04:53