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

第 8 页

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

There is also an Add Web Reference button in the lower left-hand corner of the Service Reference

Settings dialog, which enables you to add more traditional .NET Web service references. This might

be important if you have some limitations or are trying to support intersystem operability. Adding

services to your application is covered in more detail in Chapter 31 on WCF.

the toolbox

One of the major advantages over many other IDEs

that Microsoft has offered developers is true dragand-

drop placement of elements during the design

of both web and rich client applications. These

elements are all available in what is known as the

Toolbox (Ctrl+Alt+X), a tool window accessible via

the View menu, as shown in Figure 2-9.

The Toolbox window contains all of the available

components for the currently active document

being shown in the main workspace. These can be visual components, such as buttons and

textboxes; invisible, service-oriented objects, such as timers and system event logs; or even designer

elements, such as class and interface objects used in the Class Designer view.

fiGure 2-8

fiGure 2-9

Visual Studio 2010 presents the available components in groups rather than as one big mess of

components. This default grouping enables you to more easily locate the controls you need — for

example, data - related components are in their own Data group.

By default, groups are presented in List view (see the left side of Figure 2 - 9). Each component

is represented by its own icon and the name of the component. This differs from the old way of

displaying the available objects, in which the Toolbox was simply a stacked list of icons that left you

guessing as to what some of the more obscure components were, as shown with the Common Controls

group on the right side of Figure 2 - 9. You can change the view of each control group individually —

right - click anywhere within the group area and deselect the List View option in the context menu.

Regardless of how the components are presented, the

way they are used in a program is usually the same: click

and drag the desired component onto the design surface

of the active document, or double - click the component ’ s

entry for Visual Studio to automatically add an instance.

Visual components, such as buttons and textboxes,

appear in the design area where they can be repositioned,

resized, and otherwise adjusted via the property grid.

Non - visual components, such as the Timer control,

appear as icons, with associated labels, in a non - visual

area below the design area, as shown in Figure 2 - 10.

At the top left - hand side of Figure 2 - 9 is a group called Reference Library Controls with a single

component, MyControl. “ Reference_Library ” is actually the name of a class library that is defi ned

in the same solution, and it contains the MyControl control. When you start to build your own

components or controls, instead of your having to manually create a new tab and go through the

process of adding each item, Visual Studio 2010 automatically interrogates all the projects in your

solution. If any components or controls are identifi ed (essentially any class that implements System

.ComponentModel.IComponent or System.Windows.FrameworkElement for WPF), a new tab is

created for that project and the appropriate items are added with a default icon and class name (in

this case MyControl), as you can see on the left in Figure 2 - 9. For components, this is the same icon

that appears in the non - visual part of the design area when you use the component.

fiGure 2 - 10

An interesting feature of the Toolbox is that you can copy snippets of code into

the Toolbox by simply selecting a region and dragging it onto the Toolbox. You

can rename and reorder your code snippets, making it really useful for

presentations or storing chunks of code you use frequently.

Visual Studio 2010 interrogates all projects in your solution, both at startup and

after build activities. This can take a signifi cant amount of time if you have a

large number of projects. If this is the case, you should consider disabling this

feature by setting the AutoToolboxPopulate property to false under the

Windows Forms Designer node of the Options dialog (Tools . Options).

The Toolbox . 23

24 .

chaPter 2 The SoluTion explorer, Toolbox, And properTieS

To customize how your items appear in the Toolbox, you need to add a 1616 pixel bitmap to the

same project as your component or control. Next, select the newly inserted bitmap in the Solution

Explorer and navigate to the Properties window. Make sure the Build property is set to Embedded

Resource. All you now need to do is attribute your control with the ToolboxBitmap attribute:

Vb

< ToolboxBitmap(GetType(MyControl), "MyControlIcon.bmp") >

Public Class MyControl

c#

[ToolboxBitmap(typeof(MyControl), "MyControlIcon.bmp")]

public class MyControl

This attribute uses the type reference for MyControl to locate the appropriate assembly from which

to extract the MyControlIcon.bmp embedded resource. Other overloads of this attribute can use a

file path as the only argument. In this case you don’t even need to add the bitmap to your project.

Unfortunately, you can’t customize the way the automatically generated items

appear in the Toolbox. However, if you manually add an item to the Toolbox

and select your components, you will see your custom icon. Alternatively, if you

have a component and you drag it onto a form, you will see your icon appear in

the non-visual space on the designer.

It is also worth noting that customizing the Toolbox and designer experience for Windows

Presentation Foundation (WPF) controls uses the notion of a Metadata store instead of attributes.

This typically results in additional assemblies that can be used to tailor the design experience in

both Visual Studio 2010 and Expression Blend.

arranging components

Having Toolbox items in alphabetical order is a good default because it enables you to locate items

that are unfamiliar. However, if you’re only using a handful of components and are frustrated by

having to continuously scroll up and down, you can create your own groups of controls and move

existing object types around.

Repositioning an individual component is easy. Locate it in the Toolbox and click and drag it to the

new location. When you’re happy with where it is, release the mouse button and the component will

move to the new spot in the list. You can move it to a different group in the same way — just keep

dragging the component up or down the Toolbox until you’ve located the right group. These actions

work in both List and Icon views.

If you want to copy the component from one group to another, rather than move it, hold down the

Ctrl key as you drag, and the process will duplicate the control so that it appears in both groups.

Sometimes it’s nice to have your own group to host the controls and components you use the most.

To create a new group in the Toolbox, right-click anywhere in the Toolbox area and select the Add

Tab command. A new blank tab will be added to the bottom of the Toolbox with a prompt for you

The Toolbox .

25

to name it. Once you have named the tab, you can then add components to it by following the steps

described in this section.

When you first start Visual Studio 2010, the items within each group are arranged alphabetically.

However, after moving items around, you may find that they’re in a bewildering state and decide

that you simply need to start again. All you have to do is right-click anywhere within the group and

choose the Sort Items Alphabetically command.

By default, controls are added to the Toolbox according to their class names. This means you

end up with some names that are hard to understand, particularly if you add COM controls to

your Toolbox. Visual Studio 2010 enables you to modify a component’s name to something more

understandable.

To change the name of a component, right-click the component’s entry in the Toolbox and select the

Rename Item command. An edit field will appear inline in place of the original caption, enabling

you to name it however you like, even with special characters.

If you’ve become even more confused, with components in unusual groups, and you have lost sight

of where everything is, you can choose Reset Toolbox from the same right-click context menu.

This restores all of the groups in the Toolbox to their original states, with components sorted

alphabetically and in the groups in which they started.

Remember: Selecting Reset Toolbox deletes any of your own custom-made

groups of commands, so be very sure you want to perform this function!

adding components

Sometimes you’ll find that a particular component you need is not present in the lists displayed

in the Toolbox. Most of the main .NET components are already present, but some are not.

For example, the WebClient class component is not displayed in the Toolbox by default.

Managed applications can also use COM components in their design. Once added to the Toolbox,

COM objects can be used in much the same way as regular .NET components, and if coded

correctly you can program against them in

precisely the same way, using the Properties

window and referring to their methods, properties,

and events in code.

To add a component to your Toolbox layout, right-

click anywhere within the group of components

you want to add it to and select Choose Items.

After a moment (this process can take a few

seconds on a slower machine, because the machine

needs to interrogate the .NET cache to determine

all the possible components you can choose from),

you are presented with a list of .NET Framework

components, as Figure 2-11 shows.

fiGure 2-11

26 .

chaPter 2 The SoluTion explorer, Toolbox, And properTieS

Scroll through the list to locate the item you want to add to the Toolbox and check the

corresponding checkbox. You can add multiple items at the same time by selecting each of them

before clicking the OK button to apply your changes. At this time you can also remove items from

the Toolbox by deselecting them from the list. Note that this removes the items from any groups to

which they belong, not just from the group you are currently editing.

If you’re finding it hard to locate the item you need, you can use the Filter box, which filters the list

based on name, namespace, and assembly name. On rare occasions the item may not be listed at all.

This can happen with nonstandard components, such as ones that you build yourself or that are not

registered in the Global Assembly Cache (GAC). You can still add them by using the Browse button

to locate the physical file on the computer. Once you’ve selected and deselected the items you need,

click the OK button to save them to the Toolbox layout.

COM components, WPF components, Silverlight Components, and (Workflow) Activities can be

added in the same manner. Simply switch over to the relevant tab in the dialog window to view

the list of available, properly registered COM components to add. Again, you can use the Browse

button to locate controls that may not appear in the list.

ProPerties

One of the most frequently used tool windows built into Visual Studio 2010 is the Properties

window (F4), as shown in Figure 2-12. The Properties window is made up of a property grid and is

contextually aware, displaying only relevant properties of the currently selected item, whether that

item is a node in the Solution Explorer or an element in the form design area. Each line represents

a property with its name and corresponding value in two columns. The right side of Figure 2-12

shows the updated property grid for WPF applications, which includes a preview icon and search

capabilities.

fiGure 2-12

The Properties window is capable of grouping properties, or sorting them alphabetically — you

can toggle this layout using the first two buttons at the top of the Properties window. It has built-in

editors for a range of system types, such as colors, fonts, anchors, and docking, which are invoked

Properties .

27

when you click into the value column of the property to be changed. When a property is selected, as

shown in the center of Figure 2-12, the property name is highlighted and a description is presented

in the lower region of the property grid.

In the Properties window, read-only properties are indicated in gray and you will not be able to

modify their values. The value SayHello for the Text property on the left side of Figure 2-12 is

boldfaced, which indicates that this is not the default value for this property. Similarly on the right

side of Figure 2-12 the Text property has a filled-in black square between the property name and

value, indicating the value has been specified. If you inspect the following code that is generated by

the designer, you will notice that a line exists for each property that is boldfaced in the property

grid — adding a line of code for every single property on a control would significantly increase the

time to render the form.

Vb

Me.btnSayHello.Location = New System.Drawing.Point(12, 12)

Me.btnSayHello.Name = "btnSayHello"

Me.btnSayHello.Size = New System.Drawing.Size(100, 23)

Me.btnSayHello.TabIndex = 0

Me.btnSayHello.Text = "Say Hello!"

Me.btnSayHello.UseVisualStyleBackColor = True

Code snippet Form1.Designer.vb

c#

this.btnSayHello.Location = new System.Drawing.Point(12, 12);

this.btnSayHello.Name = "btnSayHello";

this.btnSayHello.Size = new System.Drawing.Size(100, 23);

this.btnSayHello.TabIndex = 0;

this.btnSayHello.Text = "Say Hello!";

this.btnSayHello.UseVisualStyleBackColor = true;

Code snippet Form1.Designer.cs

For Web and WPF applications, the properties set in the Properties window are

persisted as markup in the aspx or xaml file, respectively. As with the Windows

forms designer, only those values in the Properties window that have been set are

persisted into markup.

In addition to displaying properties for a selected item, the Properties window also provides a design

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