Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 494
nivlam Avatar asked Mar 21 '10 03:03

nivlam


2 Answers

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!

like image 74
itowlson Avatar answered Sep 20 '22 12:09

itowlson


Yes, this is possible, but you will have to hand edit the project file.

In the project file (open it with the XML Editor) locate the file listing item group. In my example, I left the form as "Form1.cs". Add the child element "<DependentUpon>" to your extended class as per the example below:

 <Compile Include="Form1.cs">
      <SubType>Form</SubType>
    </Compile>
    <Compile Include="Form1.Designer.cs">
      <DependentUpon>Form1.cs</DependentUpon>
    </Compile>
    <Compile Include="Form1.Designer.Other.cs">
      <DependentUpon>Form1.cs</DependentUpon>
      <SubType>Form</SubType>
    </Compile>

Typically though you wouldn't want any non-generated code to be hidden as a child node though. My normal practice is to create a folder in the project called "Partial Classes" and add them all in the same location.

like image 30
RobS Avatar answered Sep 18 '22 12:09

RobS