Setting Object Properties Using the Properties Window


When developing the interface of a project, you'll spend a lot of time viewing and setting object properties using the Properties window (see Figure 2.8). The Properties window contains four items:

  • An object drop-down list

  • A list of properties

  • A set of tool buttons used to change the appearance of the properties grid

  • A section showing a description of the selected property

Figure 2.8. Use the Properties window to view and change properties of forms and controls.


Selecting an Object and Viewing Its Properties

The drop-down list at the top of the Properties window contains the name of the form with which you're currently working and all the objects on the form (the form's controls). To view the properties of a control, select it from the drop-down list or find it on the form and click it. Remember that you must have the pointer item selected in the toolbox to click an object to select it.

Viewing and Changing Properties

The first two buttons in the Properties window (Categorized and Alphabetic) enable you to select the format in which you view properties. When you select the Alphabetic buttonthe selected object's properties appear in the Properties window in alphabetical order. When you click the Categorized button, all the selected object's properties are listed by category. The Appearance category, for example, contains properties such as BackColor and BorderStyle. When working with properties, select the view you're most comfortable with and feel free to switch back and forth between the views.

The Properties pane of the Properties window is used to view and set the properties of a selected object. You can set a property in one of the following ways:

  • Type in a value.

  • Select a value from a drop-down list.

  • Click a Build button for property-specific options.

By the Way

Many properties can be changed by more than one of these methods. For example, color properties supply a drop-down list of colors, but you can enter a numeric color value as well.


To better understand how changing properties works, follow these steps:

1.

Add a new text box to the form now by double-clicking the TextBox tool in the toolbox. You're now going to change a few properties of the new text box.

2.

Select the (Name) property in the Properties window by clicking it. (If your properties are alphabetic, it will be at the top of the list, not with the N's.) Type in a name for the text boxcall it txtComments.

3.

Click the BorderStyle property and try to type in the word Big. You can't; the BorderStyle property only supports selecting values from a list, though you can type a value that exists in the list. When you select the BorderStyle property, a drop-down arrow appears in the value column. Click this arrow now to display a list of the values that the BorderStyle property accepts. Select FixedSingle and notice how the appearance of the text box changes. To make the text box appear three-dimensional again, open the drop-down list and select Fixed3D.

By the Way

If you are running your display using the Windows XP Theme, controls won't take on a 3D appearancethey'll appear flat with a light blue border. I'm a big fan of this newer interface, and all the figures in this book were captured running with the Windows XP Theme enabled.

4.

Select the BackColor property, type the word guitar, and press the Tab key to commit your entry. Visual C# displays a message telling you the property value isn't valid. This happens because although you can type in text, you're restricted to entering specific values. In the case of BackColor, the value must be a named color or a number that forms an RGB value (Red, Green, Blue). For example, to change the BackColor to blue, you could use the value 0,0,255 (0 reg, 0 green, and full blue). Clear out the text and then click the drop-down arrow of the BackColor property and select a color from the drop-down list. (Selecting colors using the color palette is discussed later in this hour, and detailed information on using colors is provided in Hour 18, "Working with Graphics").

5.

Select the Font property. Notice that a Build button appears (a small button with three dots on it). When you click the Build button, a dialog box specific to the property you've selected appears. In this instance, a dialog box that enables you to manipulate the font of the text box appears (see Figure 2.9). Different properties display different dialog boxes when you click their Build buttons. Feel free to change the font and then close the window.

Figure 2.9. The Font dialog box allows you to change the appearance of text in a control.


6.

Scroll down to the Size property and notice that it has a plus sign next to it. This indicates that the property has one or more subproperties. Click the plus sign to expand the property, and you'll see that Size is composed of Width and Height.

By simply clicking a property in the Properties window, you can easily tell the type of input the property requires.

Working with Color Properties

Properties that deal with colors are unique in the way in which they accept values, yet all color-related properties behave the same way. In Visual C# 2005, colors are expressed as a set of three numbers, each number having a value from 0 to 255. A given set of numbers represents the red, green, and blue (RGB) components of a color, respectively. The value 0,255,0, for example, represents pure green, whereas the value 0,0,0 represents black and 255,255,255 represents white. In some cases, colors have also been given specific names that you can use. (See Hour 18 for more information on the specifics of working with color.)

A color rectangle is displayed for each color property in the Properties window; this color is the selected color for the property. Text is displayed next to the colored rectangle. This text is either the name of a color or a set of RGB values that define the color. Clicking in a color property causes a drop-down arrow to appear, but the drop-down you get by clicking the arrow isn't a typical drop-down list. Figure 2.10 shows what the drop-down list for a color property looks like.

Figure 2.10. The color drop-down list enables you to select from three sets of colors.


The color drop-down list is composed of three tabs: Custom, Web, and System. Most color properties use a system color by default. Hour 5, "Building FormsThe Basics," goes into great detail on system colors. I only want to mention here that system colors vary from computer to computer; they're determined by the user when he right-clicks the desktop and chooses Properties from the desktop's shortcut menu. Use a system color when you want a color to be one of the user's selected system colors. When a color property is set to a system color, the name of the system color appears in the property sheet.

The Custom tab, shown in Figure 2.11, is used to specify a specific color, regardless of the user's system color settings; changes to system colors have no effect on the property. The most common colors appear on the palette of the Custom tab, but you can specify any color you want.

Figure 2.11. The Custom tab of the color drop-down list lets you specify any color imaginable.


By the Way

The colors visible in the various palettes are limited by the number of colors that can be produced by your video card. If your video card doesn't support enough colors, some will appear dithered, which means they will appear as dots of colors rather than as a true, solid color. Keep this in mind as you develop your applications: What looks good on your computer might turn to mush if a user's display isn't as capable.


The bottom two rows in the Custom color palette are used to mix your own colors. To assign a color to an empty color slot, right-click a slot in one of the two rows to access the Define Color dialog box (see Figure 2.12). Use the controls on the Define Color dialog box to create the color you want, and then click Add Color to add the color to the color palette in the slot you selected. In addition, the custom color is automatically assigned to the current property.

Figure 2.12. The Define Color dialog box enables you to create your own colors.


The Web tab is used in web applications to pick from a list of browser-safe colors. You can, however, use these colors even if you're not creating a web application.

Viewing Property Descriptions

It's not always immediately apparent just exactly what a property is or doesespecially for new users of Visual C#. The Description section at the bottom of the Properties window shows a simple description of the selected property. To view a description, click a property or value area of a property. For a more complete description of a property, click it once to select it and then press F1 to display Help about the property.

You can hide or show the Description section of the Properties window at any time by right-clicking anywhere within the Properties window (other than in the value column or on the title bar) to display the Properties window shortcut menu and choosing Description. Each time you do this, you toggle the Description section between visible and hidden. To change the size of the Description box, click and drag the border between it and the Properties pane.




Sams Teach Yourself Microsoft Visual C# 2005 in 24 Hours, Complete Starter Kit
Sams Teach Yourself Visual C# 2005 in 24 Hours, Complete Starter Kit
ISBN: 0672327406
EAN: 2147483647
Year: N/A
Pages: 248
Authors: James Foxall

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net