Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
309 views
in Technique[技术] by (71.8m points)

c# - Very Simple definition of InitializeComponent(); Method

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...