15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Creating a Windows forms application ❘ 1121<br />

Notice the call to InitializeComponent(). InitializeComponent() is located in Form1.Designer.cs <strong>and</strong><br />

does pretty much what it describes, <strong>and</strong> that is to initialize any controls that might have been added to the form.<br />

It also initializes the form properties. For this example, InitializeComponent() looks like the following:<br />

private void InitializeComponent()<br />

{<br />

this.components = new System.ComponentModel.Container();<br />

this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;<br />

this.Text = "Form1";<br />

}<br />

As you can see, it is basic initialization code. This method is tied to the Designer in Visual Studio. When you<br />

make changes to the form by using the Designer, the changes are reflected in InitializeComponent().<br />

If you make any type of code change in InitializeComponent(), the next time you make a change<br />

in the Designer, your changes will be lost. InitializeComponent() gets regenerated after each change in<br />

the Designer. If you need to add additional initialization code for the form or controls <strong>and</strong> components<br />

on the form, be sure to add it after InitializeComponent() is called. InitializeComponent()<br />

is also responsible for instantiating the controls so any call that references a control prior to<br />

InitializeComponent() will fail with a null reference exception.<br />

To add a control or component to the form, press Ctrl+Alt+X or select Toolbox from the View menu in<br />

Visual Studio .<strong>NET</strong> to display the Toolbox. Form1 should be in design mode. Right-click Form1.cs in<br />

Solution Explorer <strong>and</strong> select View Designer from the context menu. Select the Button control, <strong>and</strong> drag it to<br />

the form in the Designer. You can also double-click the control to add it to the form. Do the same with the<br />

TextBox control.<br />

Now that you have added a TextBox control <strong>and</strong> a Button control to the form, InitializeComponent()<br />

exp<strong>and</strong>s to include the following code:<br />

private void InitializeComponent()<br />

{<br />

this.button1 = new System.Windows.Forms.Button();<br />

this.textBox1 = new System.Windows.Forms.TextBox();<br />

this.SuspendLayout();<br />

//<br />

// button1<br />

//<br />

this.button1.Location = new System.Drawing.Point(77, 137);<br />

this.button1.Name = "button1";<br />

this.button1.Size = new System.Drawing.Size(75, 23);<br />

this.button1.TabIndex = 0;<br />

this.button1.Text = "button1";<br />

this.button1.UseVisualStyleBackColor = true;<br />

//<br />

// textBox1<br />

//<br />

this.textBox1.Location = new System.Drawing.Point(67, 75);<br />

this.textBox1.Name = "textBox1";<br />

this.textBox1.Size = new System.Drawing.Size(100, 20);<br />

this.textBox1.TabIndex = 1;<br />

//<br />

// Form1<br />

//<br />

this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);<br />

this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;<br />

this.ClientSize = new System.Drawing.Size(284, 264);<br />

this.Controls.Add(this.textBox1);<br />

this.Controls.Add(this.button1);<br />

this.Name = "Form1";<br />

this.Text = "Form1";<br />

this.ResumeLayout(false);<br />

this.PerformLayout();<br />

}<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!