[英]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.