简体   繁体   English

如何在C#中将Designer.cs包含到主类中

[英]How to include Designer.cs into main class in C#

When I create an empty project in Visual Studio there is a Form1.cs which contains the Subfiles Form1.designer.cs and Form1.resx. 当我在Visual Studio中创建一个空项目时,有一个Form1.cs,其中包含子文件Form1.designer.cs和Form1.resx。

When I download some sample code eg from CodeProject very often the Designer.cs isn´t there and the code of the designer is included in the Form1.cs. 当我经常从CodeProject下载一些示例代码时,Designer.cs并不存在,而Designer1.cs中包含了设计器的代码。 I tried to make it like this, but I just get an error of Visual Studio when I want to view the Form1.cs[Design] tab. 我试图使它像这样,但是当我想查看Form1.cs [Design]选项卡时,我只是收到Visual Studio错误。

How can I achieve that the designer code is in the Form1.cs and not in a seperate file? 我怎样才能做到这一点的设计师代码在Form1.cs中,而不是在一个单独的文件?

Addition: Visual Studio creates my solution file in the project folder and the project files in the subfolders. 另外:Visual Studio在项目文件夹中创建我的解决方案文件,在子文件夹中创建项目文件。 In the sample codes often all files are in the same directory. 在样本代码通常所有文件都在同一个目录。

How may I achieve that? 我该如何实现?

Addition: Here is a link to a sample Main.cs where the designer code is integrated to the Main.cs 另外:这里是到设计代码集成到一个样品Main.cs链接Main.cs

Just cut and paste the code from Form1.Designer.cs to Form1.cs 只需将代码从Form1.Designer.cs剪切并粘贴到Form1.cs

Both Form1.Designer.cs and Form1.cs implements the class Form1. Form1.Designer.cs和Form1.cs都实现类Form1。 They use the keyword partial to split the declaration in two files. 他们使用关键字partial将声明分成两个文件。 The reason is to separate the designer generated code from the code you write manually. 原因是将设计人员生成的代码与您手动编写的代码分开。

添加新的课程项目并命名

<<Class Name>Class Name>.designer.cs

You can't Include Designer class as it is generated by Visual Studio when you include controls in your form. 当您在窗体中包含控件时,不能包含Visual Studio生成的Designer类。

If you have downloaded something which doesnt have designer.cs then that form must be unused one. 如果您下载了没有designer.cs的内容,则该表格必须未使用。

Additional One. 附加一。 It is directory structure VS Creates, so u can move the project file or solution file wherever u want to have. 它是VS Creates的目录结构,因此您可以将项目文件或解决方案文件移动到您想要的任何位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM