visual-studiowinformspartial-classes

WinForm partial classes


I have a WinForm project that contains a form called MainUI. You can see that the automatically generated partial class shows up as a node under MainUI.cs. Is there a way to "move" my self created partial class MainUI.Other.cs under MainUI.cs so that it'll show as another node?

alt text


Solution

  • Close the solution in Visual Studio, and open your .csproj file in a text editor. Find MainUI.Other.cs, and add the following XML element:

    <Compile Include="MainUI.Other.cs">
      <SubType>Form</SubType>
      <DependentUpon>MainUI.cs</DependentUpon>  <!-- this is the magic incantation -->
    </Compile>
    

    Reopen the solution in Visual Studio and enjoy subnodular goodness.

    That said, you may want to reconsider whether this is a good idea. The reason the .designer.cs file is displayed as a subnode is because you won't normally need or want to open it, because it contains generated code which you'd normally view or edit through the designer. Whereas a partial class file will contain your code, that you'll want to edit and view; it may be confusing to maintenance programmers if the file is not easily visible in Solution Explorer. However, only you can know what's right for your project -- just something to bear in mind!