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.

Working with Visual studio 2010 ❘ 391<br />

In one sense, there is no difference between the code editor <strong>and</strong> the design view; they simply present<br />

different views of the same code. What actually happened when you clicked to add the TextBox to the<br />

design view is that the editor placed the preceding extra code in your <strong>C#</strong> source fi le for you. The design<br />

view simply refl ects this change because Visual Studio is able to read your source code <strong>and</strong> determine from<br />

it what controls should be around when the application starts up. This is a fundamental shift from the old<br />

Visual Basic way of looking at things, in which everything was based around the visual design. Now, your<br />

<strong>C#</strong> source code is what fundamentally controls your application, <strong>and</strong> the design view is just a different way<br />

of viewing the source code. Incidentally, if you do write any Visual Basic 2010 code with Visual Studio, you<br />

will fi nd the same principles at work.<br />

If you had wanted to, you could have worked the other way around. If you had manually added the<br />

same code to your <strong>C#</strong> source fi les, Visual Studio would have automatically detected from the code that<br />

your application contained a TextBox control <strong>and</strong> would have shown it in the design view at the<br />

designated position. It is best to add these controls visually, <strong>and</strong> let Visual Studio h<strong>and</strong>le the initial code<br />

generation — it is a lot quicker <strong>and</strong> less error - prone to click the mouse button a couple of times than to type<br />

a few lines of code!<br />

Another reason for adding these controls visually is that, to recognize that they are there, Visual Studio does<br />

need the relevant code to conform to certain criteria — <strong>and</strong> code that you write by h<strong>and</strong> might not do so.<br />

In particular, you will notice that the InitializeComponent() method that contains the code to initialize<br />

the TextBox is commented to warn you against modifying it. That is because this is the method that Visual<br />

Studio looks at to determine what controls are around when your application starts up. If you create <strong>and</strong><br />

defi ne a control somewhere else in your code, Visual Studio will not be aware of it, <strong>and</strong> you will not be able<br />

to edit it in the design view or in certain other useful windows.<br />

In fact, despite the warnings, you can modify the code in InitializeComponent() , provided that you are<br />

careful. There is generally no harm in changing the values of some of the properties, for example, so that a<br />

control displays different text or so that it is a different size. In practice, the developer studio is pretty robust<br />

when it comes to working around any other code you place in this method. Just be aware that if you make<br />

too many changes to InitializeComponent() , you do run the risk that Visual Studio will not recognize<br />

some of your controls. We should stress that this will not affect your application in any way whatsoever<br />

when it is compiled, but it might disable some of the editing features of Visual Studio for those controls.<br />

Hence, if you want to add any other substantial initialization, it is probably better to do so in the Form1<br />

constructor or in some other method.<br />

The Properties Window<br />

This is another window that has its origins in the old Visual Basic IDE. You know from the fi rst part of<br />

the book that .<strong>NET</strong> classes can implement properties. In fact, as you will discover when building Windows<br />

Forms (see Chapter 39), the .<strong>NET</strong> base classes that represent forms <strong>and</strong> controls have a lot of properties<br />

that defi ne their action or appearance — properties such as Width , Height , Enabled (whether the user can<br />

type input to the control), <strong>and</strong> Text (the text displayed by the control) — <strong>and</strong> Visual Studio knows about<br />

many of these properties. The Properties window, shown in Figure 16 - 19, appears <strong>and</strong> allows you to edit<br />

the initial values of most of these properties for the controls that Visual Studio has been able to detect by<br />

reading your source code.<br />

The Properties window can also show events. You can view events for what you are<br />

focused on in the IDE or selected in the drop-down list box directly in the Properties<br />

window by clicking the icon that looks like a lightning bolt at the top of the window.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!