简体   繁体   English

如何添加自己的代码以及表单设计器生成的代码?

[英]How can I add my own code along with the code generated by the form designer?

When I am working in Visual Studio, and use the designer, VS manages the code for creating and positioning the controls. 当我在Visual Studio中工作并使用设计器时,VS管理用于创建和定位控件的代码。

But what if I want to add my own code there? 但是,如果我想在那里添加自己的代码怎么办?

Say I want to use a string variable as the name for my form, or for the default text in a box? 假设我想使用字符串变量作为表单名称或框中的默认文本? I know I can set this in the form_load function, but what if I want to do it in the designer code page? 我知道可以在form_load函数中进行设置,但是如果我想在设计器代码页中进行设置呢?

When the designer generates this code, it is arranged in such a way that the designer can add to it. 当设计人员生成此代码时,其设计方式可以使设计人员可以对其进行添加。 How can I add my own code, or manage parts of the code, without interfering with the functionality of the designer? 如何添加自己的代码或管理部分代码,而又不影响设计器的功能?

Would it work if I moved parts of the code to a different file? 如果我将部分代码移动到另一个文件,是否可以正常工作? How can I do that? 我怎样才能做到这一点?

Basically, I want to Have My Designer and Code it Too! 基本上,我想拥有我的设计师并为其编写代码!

Visual Studio should create the designer as a "partial" class. Visual Studio应该将设计器创建为“部分”类。 Just create another file that is also another part of the same "partial" class. 只需创建另一个文件,该文件也是同一“部分”类的另一部分。 It will be separate file, but same class. 这将是单独的文件,但是是相同的类。 Also, the autogenerator won't overwrite your code this way. 另外,自动生成器不会以这种方式覆盖您的代码。

Right click on the form and click on 'View Code'. 右键单击表单,然后单击“查看代码”。 Adding your code there will not interfere with the designer don't worry! 不用担心,在这里添加代码不会干扰设计人员!

To change the name or any property of a textbox (or any control) simply click on the textbox in the Form/Designer and change any of its property in the properties box, usually at the bottom right of the screen. 要更改文本框(或任何控件)的名称或任何属性,只需在“表单/设计器”中单击文本框,然后在属性框中更改其任何属性,通常在屏幕的右下角。

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

相关问题 如何创建自己的表单设计器? - How can I create my own form designer? C#Form Designer-生成的代码问题 - c# Form Designer - generated code problem 如何将我的属性添加到代码生成的Linq2Sql类属性? - How can I add my attributes to Code-Generated Linq2Sql classes properties? 如何构建自己的自定义代码片段? - How can I build my own customzied code snippet? 继承的控件无法在Form Designer生成的代码中生成代码 - Inherited control not generate code in form designer generated code 是否有可以添加到类中的属性,因此它将被编辑为代码,而不是在设计器中? - Is there an attribute I can add to a class so it will be edited as code, not in the designer? 不使用VS设计器时,是否应该向Form1.designer.cs添加代码? - When not using VS designer, should I add code to Form1.designer.cs? 每个表单更改都必须更正Form Designer生成的代码名称空间 - Form Designer Generated Code namespaces have to be corrected every form change Visual Studio中的表单设计器会覆盖我的代码 - Form designer in visual studio overrides my code 当我创建一个继承自 Form 的新类时,Visual Studio 会更改设计器生成的代码 - Visual Studio changes designer generated code when I create a new class inheriting from Form
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM