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 an object's characteristics. 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 it to. To display an object's properties, 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 contains the form's name: Form1 System.Windows.Forms.Form. Form1 is the name of the object, and System.Windows.Forms.Form is the type of object.
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.
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).
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 form's name but also to change its filename. Change the programmable name and the filename at the same time by following these steps:
- Click the Name property and change the text from Form1 to ViewerForm. Notice that this does not change the form's filename as it's displayed in the Solution Explorer window located above the Properties window.
- Right-click Form1.cs in the Solution Explorer window (the window above the properties window).
- Choose Rename from the context menu that appears.
- Change the text from Form1.cs to ViewerForm.cs.
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 lot—for 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. 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 form's name. 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:
- Click the form once more so that its properties appear in the Properties window.
- Use the scrollbar in the Properties window to locate the Text property.
- 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, you would lose all your work up to this point. Get into the habit of frequently saving your work, which commits your changes to disk.
Click the Save All button on the toolbar (the picture of a stack of disks) 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. You can have Visual C# create a solution folder in which the project folder gets placed. On large projects, this is a handy feature. For now, it's an unnecessary step, so uncheck the Create Directory for Solution box and then 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 icons—the little pictures that represent programs. Icons most commonly appear in the Start menu next to the names of their respective programs. In Visual C#, 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.
To give the form an icon, follow these steps:
- In the Properties window, click the Icon property to select it.
- When you click the Icon property, a small button with three dots appears to the right of the property. Click this button.
- 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 and 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.
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.
Save the project now by choosing File, Save All from the menu or by clicking the Save All button on the toolbar—it has a picture of stacked disks on it.