Partial Form Class C# - Only display code view for class

戏子无情 提交于 2021-01-27 11:30:55

问题


I have a Blank Forms project in C#.

I wanted to separate the functions and events into different Codefiles on the Form Class. In order to make it more manageable when it becomes large and multiple people are using it on CodeControl.

So I created;

  • Form1.Functions.cs
  • Form1.Events.cs

These extra partial classes both consist of

namespace MyLargeProject
{
    public partial class Form1
    {

    } 

}

I also changed the csproj file so the VS IDE would make them appear in the Soution Explorer.

Example

<Compile Include="Forms\Form1.Functions.cs">
  <DependentUpon>Form1.cs</DependentUpon>
  <SubType>Form</SubType>
</Compile>

So the program compiles and all is looking good except that when I open the file from the solution explorer it opens up a blank Form designer, and not the code.

I want VS to only open this class as a code file to limit issues with the Form1.Designer.cs file.

I have also tried removing the SubType

  <SubType>Form</SubType>

but Visual Studio keeps adding it back to the csproj file.


回答1:


As mentioned on the comments, Visual Studio will open any file ending with .Designer.cs in the code editor rather than on the forms designer. Taking advantage of this, a good solution to have a form not open in the form designer is to name the file that contains the class with ending .Designer.cs.

Note: I would say this is not elegant. I find it counter intuitive to have a file named Form1.Designer.cs and don't have a designer for it. Also, if the form was originally created with the forms designer, this will require to merge Form1.cs and Form1.Designer.cs which can be problematic from within Visual Studio.


My original answer

Follow these steps:

1) Make sure the designer is closed.

2) Modify your Form1.Designer.cs and add the attribute DesignerCategory with the empty string "" (any random value other than "Designer", "Form" or "Component" also works - case insensitive):

namespace MyLargeProject
{
    [System.ComponentModel.DesignerCategory("")]
    public partial class Form1
    {
        // ...
    }
}

3) Unload your project.

4) Reload your project.

5) Notice the designer doesn't appear.

6) Enjoy!

Note: tested on Visual Studio 2012, yet based on a discussion thread from 2005.



来源:https://stackoverflow.com/questions/20796628/partial-form-class-c-sharp-only-display-code-view-for-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!