InitializeComponent() 的非常简单的定义;方法 [英] Very Simple definition of InitializeComponent(); Method

查看:24
本文介绍了InitializeComponent() 的非常简单的定义;方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读 Head First C# 一书,并使用了 InitializeComponent();方法多次.

一个例子是在派对策划练习中我创建了一个名为 DinnerParty.cs 的类,然后在 Form1.cs 中使用了这个代码

public Form1(){初始化组件();DinnerParty = new DinnerParty() { NumberOfPeople = 5 };DinnerParty.SetHealthyOption(checkBox2.Checked);DinnerParty.CalculateCostOfDecorations(checkBox1.Checked);DisplayDinnerPartyCost();}

我的问题是,Initialize Component 方法到底在做什么.我的理解是,我正在定义 DinnerParty 类的一个新对象或实例并设置所有值,到目前为止,我假设 InitializeComponent() 是在说使用以下内容设置我的字段的值:"

我能不能有一个基本的,我可以理解定义的东西.我看过以前的帖子和关于这个的答案,一切都太复杂了.我会标记最容易理解但仍然包含关键信息的回复作为答案.

解决方案

InitializeComponent 是一种在您创建/更改表单时由表单设计器自动为您编写的方法.

每个表单文件(例如 Form1.cs)都有一个设计器文件(例如 Form1.designer.cs),其中包含 InitializeComponent 方法、泛型 Form.Dispose 的覆盖以及您所有的用户界面对象,如按钮、文本框、标签和表单本身.

InitializeComponent 方法包含使用您(程序员)使用表单设计器的属性网格使用您(程序员)提供的值创建和初始化在表单表面上拖动的用户界面对象的代码.因此在调用 InitializeComponent 之前不要尝试与表单或控件交互.
此外,您还可以在此处找到将控件和表单事件链接到您为响应用户操作而编写的特定事件处理程序所需的管道.

由于 部分类 可以像单个代码块一样将两个或多个代码文件保存在一起.

当然,由于表单设计器执行了大量更改,因此不要尝试手动修改此方法是一个非常好的建议,而有时,我发现添加Dispose 方法的代码,目的是销毁在表单生命周期中创建的一些非托管对象.

I have been working through the Head First C# book and have used the InitializeComponent(); method several times.

An example of this is on the Party Planner exercise I created a class called DinnerParty.cs and then used this code in the Form1.cs

public Form1()         
        {        
            InitializeComponent(); 
            dinnerParty = new DinnerParty() { NumberOfPeople = 5 };
            dinnerParty.SetHealthyOption(checkBox2.Checked);
            dinnerParty.CalculateCostOfDecorations(checkBox1.Checked);
            DisplayDinnerPartyCost();
        }

My Question is, what exactly is the Initialize Component method doing. My understanding is that I am defining a new object or instance of the DinnerParty class and setting up all the values, so far I have assumed that InitializeComponent() is kind of saying "Set up values of my fields using the following:"

Could I please have a BASIC, something I can get my head around definition. I have looked at previous posts and answers regarding this and everything is too complex. I will mark the easiest to understand response that still has the key information as the answer.

解决方案

InitializeComponent is a method automatically written for you by the Form Designer when you create/change your forms.

Every Forms file (e.g. Form1.cs) has a designer file (e.g. Form1.designer.cs) that contains the InitializeComponent method, the override of the generic Form.Dispose, and the declaration of all of your User Interface objects like buttons, textboxes, labels and the Form itself.

The InitializeComponent method contains the code that creates and initializes the user interface objects dragged on the form surface with the values provided by you (the programmer) using the Property Grid of the Form Designer. Due to this fact do not ever try to interact with the form or the controls before the call to InitializeComponent.
Also, you will find here, the plumbing required to link the controls and form events to the specific event handlers you have written to respond to the user actions.

The code contained in Form1.cs and the Form1.Designer.cs files is part of the same class thanks to the concept of partial classes that could keep two or more files of your code together like a single block of code.

Of course, due to the high numbers of changes executed by the Form Designer, it is a really good advice to not try to modify manually this method, while, sometime, I find useful to add code to the Dispose method with the purpose to destroy some unmanaged objects created in the form lifetime.

这篇关于InitializeComponent() 的非常简单的定义;方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆