TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# InitializeComponent Method: Windows Forms

InitializeComponent. In Windows Forms we create programs visually. We drag controls to the Form in Visual Studio. Behind the scenes, Visual Studio adds code to the InitializeComponent method, which is called in the Form constructor.Form
Example. In this code sample, we see the InitializeComponent method when a new program is created. Then, we see InitializeComponent after adding a Button control by dragging one to the Window in the Designer.

Also: We see that statements for button1 were added to the contents. Visual Studio added the SuspendLayout and ResumeLayout method calls.

Initial contents of InitializeComponent: C# /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.Text = "Form1"; } Contents after adding Button: C# /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.button1 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // button1 // this.button1.Location = new System.Drawing.Point(13, 13); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(75, 23); this.button1.TabIndex = 0; this.button1.Text = "button1"; this.button1.UseVisualStyleBackColor = true; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(292, 273); this.Controls.Add(this.button1); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false); }
Do not modify. It is best not to modify the InitializeComponent method, but in some cases it may be useful to. Sometimes, when creating a program you may remove a Control and the InitializeComponent method will no longer compile.

And: In this case, you can find the compilation error and fix it, usually by removing lines.

Discussion. When you create a new program, the InitializeComponent() call is located in the Form1 constructor body. Should you add code before or after this call? If the code doesn't interact with the controls, either location is fine.

However: If the code does interact with the controls, you will want to put the code after the InitializeComponent call.

Also, you can create a Form1_Load event handler. You can do this by double-clicking on the Form in the Designer. This will run after the Form1 constructor. This is a good way to separate your code from the InitializeComponent call.
Summary. We looked at the InitializeComponent method in the C# language and Windows Forms platform. The InitializeComponent method call is implemented with a partial class to make your part of the code easier to edit.partial
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf