如何将一个Form中的代码拆分到多个文件中

https://social.msdn.microsoft.com/Forums/en-US/64c77755-b0c1-4447-8ac9-b5a63a681b78/partial-class-for-a-form-problem?forum=csharpide

首先,新建一个windows窗体应用程序

添加一个FormMain,然后可以看到这个FormMain下面,有一个FormMain.Designer.cs文件,里面存放了控件初始化的代码

并且双击FormMain.Designer.cs文件的时候。不会显示窗体,只有在点击FormMain.cs的时候,才会显示窗体

 

步骤:

1.首先添加一个新的类,FormMain.Tools.cs

2.给这个文件中的FormMain添加partial关键字修饰

3.用notepad++打开项目文件,然后编辑

<Compile Include="UI\FormMain.Tools.cs">
<DependentUpon>FormMain.cs</DependentUpon>
</Compile>

 

上面操作完成以后,文件会显示在FormMain.cs的文件夹下

双击FormMain.Tools.cs的时候。发现居然显示了一个完整的窗体。

按照上面的链接处理,给FormMain.Tools.cs添加了Attribute

[System.ComponentModel.DesignerCategory("code")]

 

不过,这个方法是针对整个类而言的。会导致FormMain.cs也不显示窗体

 

 

最后,按照另外一个方法处理:

 

Trying like this:

 

1) Create new Windows Application with "Form1.cs" and "Form1.Designer.cs"

 

2) Add new class item "PanelControl.Designer.cs"

 

3) Change the class name in "PanelControl.Designer.cs" to a Form1 partial class like this

 

     partial class Form1

 

4) Close "PanelControl.Designer.cs" then double click on it again from solution panel.

 

It should be your intention.

 

 

让新添加的文件,以.Designer.cs结尾就可以了

 

posted @ 2015-10-21 09:58  ChuckLu  阅读(1583)  评论(0编辑  收藏  举报