Every control has a set of
properties and events that define how the control acts when your
application runs. For example, the button control's Text property specifies the text that's displayed on the button. The button control has a Click event that's executed each time a user clicks the button.
The Visual Studio Properties window displays a control's properties and events in a grid, as shown in Figure 1.
The Properties window is open by default. If you
close the Properties window at some point, you have two options for
opening it again:
The Properties window has the following elements:
A drop-down list at the top displays the selected control. Use the list to select a control to view.
A toolbar changes the view settings. Your view-setting choices are, from left to right
Categorized: Properties appear in groups
Alphabetical: Properties are sorted in alphabetical order
Properties: The control's properties are displayed
Events: The control's events are displayed
Two columns display the name-value pairs of the control's properties or events, depending on the view setting.
A brief description of the selected property or event appears at the bottom of the window.
The Properties window is a visual tool that organizes
and categorizes the thousands of properties and events available for
manipulating controls. You use the Properties window for two tasks:
When you use the Properties window, Visual Studio
generates the code to set the control's properties and create event
handlers. You can do all these tasks by using code, although it's much
easier to use Visual Studio.
You can access thousands of properties and events by
using the Properties window. Visual Studio groups properties and events
into categories so that they're easier to manage. The following list
describes the properties and events categories:
Action: Creates handlers for events, such as Click and Scroll.
Accessibility:
Sets properties that make controls accessible to people with
disabilities and makes the application easier to use for everyone.
Appearance: Contains properties related to font, color, border, and cursor style. The Text property and text-formatting properties are in this group.
Behavior: Contains properties and events related to how a control acts. Examples include Sorted, TabStop, and SelectedIndexChanged.
Data: Sets a control's data source, gets items selected in a control, and responds to changes in the data.
Design: Contains design-time properties, such as whether controls are locked. The Name property is here, too.
DragDrop: Responds to events related to dragging and dropping controls.
Focus: Captures events related to how the control gains and loses focus.
Key: Responds to keyboard-related events.
Layout: Sets layout properties, such as anchor, margin, and size.
Mouse: Responds to mouse events, such as MouseUp.
WindowStyle: Sets properties that define the Windows Form itself.
Within each control are properties that you can set. Here are some common properties that many controls share:
To set the access key for a control, add an ampersand (&) in front of the letter to use as the access key in the Text property. For example, to set the letter P in Print as the access key, set the Text property for the control equal to &Print. A user can then press the Alt key in combination with the access key to execute the control.
|
|
NOTE
Windows Forms are controls. You access the
properties of a Windows Forms control the same way you access the
properties for any control. Here are the common properties for Windows
Forms:
Text: Sets the text displayed on the form's title bar.
DefaultButton: Sets the button that responds to the Enter key by default.
CancelButton: Sets the button that responds to the Esc key by default.
MainMenuStrip: Sets the primary menu control for the form.
In addition to the controls you use to customize your
forms, you have events to consider. Events are actions that your
application might handle or respond to. Examples of common events
include
Click
KeyDown
Load
ValueChanged
You can get context-sensitive help for any property
or event you see in the Properties window by positioning your cursor in
the value field for the property or event, and pressing F1.
|
|
1. Setting properties
Properties are expressed as name-value pairs. A property's value isn't always as simple as color=black.
Values can be complex. You can use the selectors, wizards, and ellipses
on the Visual Studio Properties page to dynamically build complex
property values.
Examples of complex properties include properties related to
Setting data sources
Editing collections of items
Applying fonts and formatting
Selecting images and files
To set properties for a control on a Windows Form, follow these steps:
Click the control in the forms designer.
Press F4 to display the Properties window.
The control's name and type appear on the drop-down list at the top of the Properties window.
Click the Properties button on the Properties window's toolbar to display a list of the control's properties.
The Properties button is the third toolbar button from the left.
Scroll down the properties list and enter property values.
You
can type simple property values or select them from a drop-down list.
Click the ellipsis button for more complex properties, such as
collections to display a dialog box that builds the property, as shown
in Figure 2. Click the plus sign (+) next to compound properties to set individual properties.
2. Responding to events
The Visual Studio Properties window displays a control's events. For example, when a user clicks a button, the button's Click event occurs. You can use event handlers to write code that fires each time the event fires.
To set an event handler by using the Properties window, follow these steps:
Select a form's control by clicking the control.
In the Properties window, click the Events button to display the control's events.
To
create an event handler by using a default name, either press Enter
without typing a name or double-click the field for the name.
Double-click a control on the Windows Forms Designer to create the control's default event handler.
|
|
Type a name for the event handler next to the event and press Enter.
Visual Studio creates the event handler and opens the code editor to the event handler.