Changing the Characteristics of Objects


Almost everything you work with in Visual C# is an object. Forms, for instance, are objects, as are all the items you can put on a form to build an interface such as list boxes and buttons. There are many types of objects, and objects are classified by type.

For example, a form is a Form object, whereas items you can place on a form are called Control objects, or controls. (Hour 3, "Understanding Objects and Collections," discusses objects in detail.) Some objects don't have a physical appearance but exist only in code, and you'll learn about these kinds of objects in later hours.

Every object has a distinct set of attributes known as properties (regardless of whether the object has a physical appearance). You have certain properties about you, such as your height and hair color. Visual C# objects have properties as well, such as Height and BackColor. Properties define the characteristics of an object. When you create a new object, the first thing you need to do is set its properties so that the object appears and behaves the way you want. To display the properties of an object, click the object in its designer (the main work area in the IDE).

First, make sure your Properties Window is displayed by opening the View menu and choosing Properties Window. Next, click anywhere in the default form now (its title bar says Form1) and check to see whether its properties are displayed in the Properties window. You'll know because the drop-down list box at the top of the properties window will contain the form's name: Form1 System.Windows.Forms.Form. Form1 is the name of the object, while System.Windows.Forms.Form is the type of object.

Naming Objects

The property you should always set first for any new object is the Name property. Scroll toward the top of the properties list until you see the "(Name)" property (see Figure 1.4). If the Name property isn't one of the first properties listed, your Properties Window is set to show properties categorically instead of alphabetically. You can show the list alphabetically by clicking the Alphabetical button that appears just above the properties grid.

Figure 1.4. The Name property is the first property you should change when you add a new object to your project.


By the Way

I recommend that you keep the Properties window set to show properties in alphabetical order; doing so makes it easier to find properties that I refer to in the text. Note, the Name property always stays toward the top of the list and is referred to as (Name). If you're wondering why it has parentheses around it, that's because the parentheses force the property to the top of the list, since symbols come before letters in an alphabetical sort.


When saving a project, you choose a name and a location for the project and its files. When you first create an object, Visual C# gives the object a unique, generic name based on the object's type. Although these names are functional, they simply aren't descriptive enough for practical use. For instance, Visual C# named your form Form1, but it's common to have dozens of forms in a project, and it would be extremely difficult to manage such a project if all forms were distinguishable only by a number (Form2, Form3, and so forth).

By the Way

What you're actually working with is a form class, or template, that will be used to create and show forms at runtime. For the purpose of this quick tour, I simply refer to it as a form. See Hour 5, "Building FormsThe Basics," for more information.


To better manage your forms, give each one a descriptive name. Visual C# gives you the chance to name new forms as they're created in a project. Visual C# created this default form for you, so you didn't get a chance to name it. It's important to not only change the name of the form but also to change the filename of the form. Change the programmable name and the filename and the same time by following these steps:

1.

Click the Name property and change the text from Form1 to frmViewer. Notice that this does not change the filename of the form as it's displayed in the Solution Explorer window.

2.

Right-click Form1.cs in the Solution Explorer window (the window above the Properties window).

3.

Choose Rename from the context menu that appears.

4.

Change the text from Form1.vb to frmViewer.cs.

By the Way

I use the frm prefix here to denote that the file is a form class. Prefixes are optional, but I find they really help you keep things organized.


Did you Know?

The Name property of the form is actually changed for you automatically when you rename the file. I had you explicitly change the Name property because it's something you're going to be doing a lotfor all sorts of objects.


Setting the Text Property of the Form

Notice that the text that appears in the form's title bar says Form1. This is because Visual C# sets the form's title bar to the name of the form when it's first created but doesn't change it when you change the name of the form. The text in the title bar is determined by the value of the Text property of the form. Change the text now by following these steps:

1.

Click the form once more so that its properties appear in the Properties window.

2.

Use the scrollbar in the Properties window to locate the Text property.

3.

Change the text to Picture Viewer. Press the Enter key or click on a different property. You'll see the text in the title bar of the form change.

Saving a Project

The changes you've made so far exist only in memory; if you were to turn off your computer at this time (don't do this), you would lose all of your work up to this point. Get into the habit of frequently saving your work (committing the changes to disk).

Click the Save All button on the toolbar (the picture of a stack of diskettes) now to save your work. Visual C# then displays the Save Project dialog box shown in Figure 1.5. Notice that the Name property is already filled in because you named the project when you created it. The Location text box is where you specify the location in which to save the project. Visual C# creates a subfolder in this location using the value in the Name text box (in this case, Picture Viewer). You can use the default location, or change it to suit your purposes. Don't worry about the Solution Name text box or the check box for Create directory for solution at this time, just be sure to leave the checkbox checked as it appears in Figure 1.5. Click Save to save the project.

Figure 1.5. When saving a project, choose a name and a location for the project and its files.


Giving the Form an Icon

Everyone who has used Windows is familiar with iconsthe little pictures that represent programs. Icons most commonly appear in the Start menu next to the name of their respective programs. In Visual C# 2005, you not only have control over the icon of your program file, you can also give every form in your program a unique icon if you want to.

By the Way

The following instructions assume that you have access to the source files for the examples in this book. They are available at www.samspublishing.com. You can also get these files, as well as discuss this book, at my website at http://www.jamesfoxall.com/books.htm. When you unzip the samples, a folder will be created for each hour, and within each hour's folder will be subfolders for the sample projects. You'll find the icon in the folder Hour 1\Picture Viewer.

You don't have to use the icon I've provided for this example; you can use any icon of your choice. If you don't have an icon available (or you want to be a rebel), you can skip this section without affecting the outcome of the example.


To give the form an icon, follow these steps:

1.

In the Properties window, click the Icon property to select it.

2.

When you click the Icon property, a small button with three dots appears to the right of the property. Click this button.

3.

Use the Open dialog box that appears to locate the PictureViewer.ico file or another icon file of your choice. When you've found the icon, double-click it, or click it once to select it and then click Open.

After you've selected the icon, it appears in the Icon property along with the word "Icon." A small version of the icon appears in the upper-left corner of the form as well. Whenever this form is minimized, this is the icon displayed on the Windows taskbar.

Changing the Size of the Form

Next, you're going to change the Width and Height properties of the form. The Width and Height values are shown collectively under the Size property; Width appears to the left of the comma, Height to the right. You can change the Width or Height property by changing the corresponding number in the Size property. Both values are represented in pixels (that is, a form that has a Size property of 200,350 is 200 pixels wide by 350 pixels tall). To display and adjust the Width and Height properties separately, click the small plus sign (+) next to the Size property (see Figure 1.6).

Figure 1.6. Some properties can be expanded to show more specific properties.


By the Way

A pixel is a unit of measurement for computer displays; it's the smallest visible "dot" on the screen. The resolution of a display is always given in pixels, such as 800x600 or 1024x768. When you increase or decrease a property by one pixel, you're making the smallest possible visible change to the property.


Change the Width property to 400 and the Height to 325 by typing in the corresponding box next to a property name. To commit a property change, press Tab or Enter, or click a different property or window. Your screen should now look like the one in Figure 1.7.

Figure 1.7. Changes made in the Properties window are reflected as soon as they're committed.


By the Way

You can also size a form by dragging its border, which you'll learn about in Hour 2. This property can also be changed by program code, which you'll learn how to write in Hour 5.


Save the project now by choosing File, Save All from the menu or by clicking the Save All button on the toolbarit has a picture of stacked diskettes on it.




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