饭饭TXT > 学习管理 > 《Visual Studio 2010 高级编程(英文出书版)》作者:Nick Randolph/等【完结】 > [Visual.Studio.2010.高级编程].Professional.Visual.Studio.2010.txt

第 9 页

作者:Nick Randolph/等 当前章节:15410 字 更新时间:2026-6-18 14:51

experience for wiring up event handlers. The Properties window on the left of Figure 2-13 illustrates

the event view that is accessible via the fourth button, the lightning bolt, across the top of the

Properties window. In this case, you can see that there is an event handler for the click event. To wire

up another event you can either select from a list of existing methods via a drop-down list in the

value column, or you can double-click the value column. This creates a new event-handler method

and wires it up to the event. If you use the first method you will notice that only methods that match

the event signature are listed.

28 .

chaPter 2 The SoluTion explorer, Toolbox, And properTieS

fiGure 2-13

Certain components, such as the DataGridView, expose a number of commands, or shortcuts,

that can be executed via the Properties window. On the right side of Figure 2-13 you can see two

commands for the DataGridView: Edit Columns and Add Column. When you click either of these

command links, you are presented with a dialog for performing that action.

If the Properties window has only a small amount of screen real estate, it can be difficult to scroll

through the list of properties. If you right-click in the property grid you can uncheck the Command

and Description checkboxes to hide these sections of the Properties window.

extending the Properties window

You have just seen how Visual Studio 2010 highlights properties that have changed by boldfacing

the value. The question that you need to ask is, How does Visual Studio 2010 know what the default

value is? The answer is that when the Properties window interrogates an object to determine what

properties to display in the property grid, it looks for a number of design attributes. These attributes

can be used to control which properties are displayed, the editor that is used to edit the value, and

what the default value is. To show how you can use these attributes on your own components, start

with adding a simple automatic property to your component:

Vb

Public Property Description As String

Code snippet MyControl.vb

c#

public string Description { get; set; }

Code snippet MyControl.cs

The Browsable attribute

By default, all public properties are displayed in the property grid. However, you can explicitly

control this behavior by adding the Browsable attribute. If you set it to false the property will not

appear in the property grid.

Properties .

29

Vb

<System.ComponentModel.Browsable(False)>

Public Property Description As String

Code snippet MyControl.vb

c#

[System.ComponentModel.Browsable(false)]

public string Description { get; set; }

Code snippet MyControl.cs

Displayname attribute

The DisplayName attribute is somewhat self-explanatory; it enables you to modify the display name

of the property. In our case, we can change the name of the property as it appears in the property

grid from Description to VS2010 Description.

Vb

<System.ComponentModel.DisplayName("VS2010 Description")>

Public Property Description As String

Code snippet MyControl.vb

c#

[System.ComponentModel.DisplayName("VS2010 Description")]

public string Description { get; set; }

Code snippet MyControl.cs

Description

In addition to defining the friendly or display name for the property, it is also worth providing

a description, which appears in the bottom area of the Properties window when the property is

selected. This ensures that users of your component understand what the property does.

Vb

<System.ComponentModel.Description("My first custom property")>

Public Property Description As String

Code snippet MyControl.vb

c#

[System.ComponentModel.Description("My first custom property")]

public string Description { get; set; }

Code snippet MyControl.cs

30 .

chaPter 2 The SoluTion explorer, Toolbox, And properTieS

Category

By default, any property you expose is placed in the Misc group when the Properties window is

in grouped view. Using the Category attribute, you can place your property in any of the existing

groups, such as Appearance or Data, or a new group if you specify a group name that doesn’t exist.

Vb

<System.ComponentModel.Category("Appearance")>

Public Property Description As String

Code snippet MyControl.vb

c#

[System.ComponentModel.Category("Appearance")]

public string Description { get; set; }

Code snippet MyControl.cs

DefaultValue

Earlier you saw how Visual Studio 2010 highlights properties that have changed from their initial or

default values. The DefaultValue attribute is what Visual Studio 2010 looks for to determine the

default value for the property.

Vb

Private Const cDefaultDescription As String = "<enter description>"

<System.ComponentModel.DefaultValue(cDefaultDescription)>

Public Property Description As String = cDefaultDescription

Code snippet MyControl.vb

c#

private const string cDefaultDescription = "<enter description>";

private string mDescription = cDefaultDescription;

[System.ComponentModel.DefaultValue(cDefaultDescription)]

public string Description

{

get

{

return mDescription;

}

set

{

mDescription = value;

}

}

Code snippet MyControl.cs

Properties .

31

In this case, if the value of the Description property is set to “ < enter description > “ , Visual

Studio 2010 removes the line of code that sets this property. If you modify a property and want to

return to the default value, you can right-click the property in the Properties window and select

Reset from the context menu.

It is important to note that the DefaultValue attribute does not set the initial

value of your property. It is recommended that if you specify the DefaultValue

attribute you also set the initial value of your property to the same value, as

done in the preceding code.

ambientValue

One of the features we all take for granted but that few truly understand is the concept of ambient

properties. Typical examples are background and foreground colors and fonts: unless you explicitly

set these via the Properties window they are inherited — not from their base classes, but from their

parent control. A broader defi nition of an ambient property is a property that gets its value from

another source.

Like the DefaultValue attribute, the AmbientValue attribute is used to indicate to Visual Studio

2010 when it should not add code to the designer file. Unfortunately, with ambient properties you

can’t hard-code a value for the designer to compare the current value to, because it is contingent

on the property’s source value. Because of this, when you define the AmbientValue attribute this

tells the designer to look for a function called ShouldSerializePropertyName. In our case, it would

be ShouldSerializeDescription, and this method is called to determine whether the current

value of the property should be persisted to the designer code file.

Vb

Private mDescription As String = cDefaultDescription

< System.ComponentModel.AmbientValue(cDefaultDescription) >

Public Property Description As String

Get

If Me.mDescription = cDefaultDescription AndAlso

Me.Parent IsNot Nothing Then

Return Parent.Text

End If

Return mDescription

End Get

Set(ByVal value As String)

mDescription = value

End Set

End Property

Private Function ShouldSerializeDescription() As Boolean

If Me.Parent IsNot Nothing Then

Return Not Me.Description = Me.Parent.Text

32 .

chaPter 2 The SoluTion explorer, Toolbox, And properTieS

Else

Return Not Me.Description = cDefaultDescription

End If

End function

Code snippet MyControl.vb

c#

private string mDescription = cDefaultDescription;

[System.ComponentModel.AmbientValue(cDefaultDescription)]

public string Description{

get{

if (this.mDescription == cDefaultDescription &&

this.Parent != null){

return Parent.Text;

}

return mDescription;

}

set{

mDescription = value;

}

}

private bool ShouldSerializeDescription(){

if (this.Parent != null){

return this.Description != this.Parent.Text;

}

else{

return this.Description != cDefaultDescription;

}

}

Code snippet MyControl.cs

When you create a control with this property, the initial value would be set to the value of the

DefaultDescription constant, but in the designer you would see a value corresponding to

the Parent.Text value. There would also be no line explicitly setting this property in the designer

code file, as reflected in the Properties window by the value being non-boldfaced. If you change the

value of this property to anything other than the DefaultDescription constant, you will see that it

becomes bold and a line is added to the designer code file. If you reset this property, the underlying

value is set back to the value defined by AmbientValue, but all you will see is that it has returned to

displaying the Parent.Text value.

suMMary

In this chapter you have seen three of the most common tool windows in action. Knowing how to

manipulate these windows can save you considerable time during development. However, the true

power of Visual Studio 2010 is exposed when you start to incorporate the designer experience into

your own components. This can be useful even if your components aren’t going to be used outside

your organization. Making effective use of the designer can improve not only the efficiency with

which your controls are used, but also the performance of the application you are building.

3 3

options and Customizations

what’s in this chaPter?

.

Customizing the Visual Studio 2010 start page

.

Tweaking options

.

Controlling window layout

Now that you’re familiar with the general layout of Visual Studio 2010, it’s time to learn

how you can customize the IDE to suit your working style. In this chapter you learn how to

manipulate tool windows, optimize the code window for maximum viewing space, and change

fonts and colors to reduce developer fatigue.

As Visual Studio has grown, so too has the number of settings that you can adjust to optimize

your development experience. Unfortunately, unless you’ve periodically spent time sifting

through the Options dialog (Tools . Options), it’s likely that you’ve overlooked one or two

settings that might be important. Through the course of this chapter, you see a number of

recommendations of settings you might want to investigate further.

A number of Visual Studio add-ins will add their own nodes to the Options dialog because

this provides a one-stop shop for configuring settings within Visual Studio. Note also that

some developer setting profiles, as selected in Chapter 1, show only a cut-down list of options.

In this case, checking the Advanced checkbox shows the complete list of available options.

the start PaGe

By default, when you open a new instance of Visual Studio 2010 you see what is known

as the Start Page. You can adjust this behavior from the Environment . Startup node of

the Options dialog. Other alternatives are to display the Home Page (which you can set via the

Environment .

Web Browser node), the last loaded solution, open or new project dialogs, or

no action at all.

34 .

chaPter 3 opTionS And cuSTomizATionS

The reason that most developers stick with the Start Page is that it provides a useful starting point

from which to jump to any number of actions. In the left image of Figure 3-1, you can see that there

are links down the left side for connecting to Team Foundation Server and for creating or opening

projects. There is also a list of recent projects allowing you to quickly open projects that you have

recently been working on. Hovering the mouse over the left side of a project displays a horizontal

pin. Clicking the pin changes the orientation to vertical to indicate that the project has been pinned

to the Recent Projects list. Alternatively, you can right-click a project and either open the containing

folder (useful if you want to locate the project on disk rather than actually opening it) or remove the

project from the list. In the lower-left corner there are two checkboxes that control whether the Start

Page is closed after opening a project and whether it’s displayed at startup. If for whatever reason the

Start Page is closed and you want to open it again, you can do so by selecting the View . Start Page

menu item.

On the right side of the Start Page are a series of tabs. The Get Started tab (the left image of

Figure 3-1) contains various subsections on a variety of topics including Windows, Web, Office

and the Cloud. Click on any of these to find information on how to get started working with these

technologies and what’s new in Visual Studio 2010.

On the Guidance and Resources tab (the center image of Figure 3-1) are links to topics pertaining to

best practices, how-to’s, and other reference material on designing, building, testing and deploying

software using Visual Studio 2010.

fiGure 3-1

Finally, the Latest News tab (the right image of Figure 3-1) keeps you abreast of the latest

happenings around Visual Studio 2010 and the .NET Framework. You can either use the default

RSS feed or specify your own feed that you want to be displayed within the Start Page.

customizing the start Page

In Visual Studio 2010, the Start Page is in fact a WPF control that is hosted within the IDE shell.

As such, it is possible to tailor the Start Page to feature information or actions that are relevant to

you. Rather than modifying the default Start Page, Visual Studio supports user-specific or Custom

Start Pages. By default, this features is disabled so before you can start customizing your Start Page

you have to enable the Allow Custom Start Page checkbox on the Environment . Startup tab of the

目录
设置
设置
阅读主题
字体风格
雅黑 宋体 楷书 卡通
字体大小
适中 偏大 超大
保存设置
恢复默认
手机
手机阅读
扫码获取链接,使用浏览器打开
书架同步,随时随地,手机阅读
首 页 < 上一章 章节列表 下一章 > 尾 页