Changing the Characteristics of Objects
Almost everything you work with in Visual Basic is an object. Forms, for example, 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. 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). Properties define an object’s characteristics. You have certain properties as a person, such as your height and hair color. Visual Basic objects have properties as well, such as Height and BackColor. 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).
Click anywhere in the default form now (it’s the window with the title Form1), and check to see that 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 is the object’s name and System.Windows.Forms.Form is the object’s type.
The property you should always set first when creating any new object is the Name property. Press F4 to display the Properties window (if it’s not already visible), and scroll toward the top of the properties list until you see the (Name) property, as shown in Figure 1.4. If the Name property isn’t one of the first properties listed, the 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 also choose a name and a location for the project and its files. When you first create an object within the project, Visual Basic 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 Basic named your form Form1, but it’s common to have dozens (or even hundreds) of forms in a project. 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 Basic gives you the chance to name new forms as they’re created in a project. Visual Basic created this default form for you, so you didn’t get a chance to name it. It’s important not only to change the form’s name but also to change its filename. Change the programmable name and the filename 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.vb in the Solution Explorer window (the window above the Properties window).
Choose Rename from the context menu that appears.
Change the text from Form1.vb to ViewerForm.vb.
The form’s Name property is actually changed for you automatically when you rename the file. In future examples, I will have you rename the form file so that the Name property is changed automatically. I had you set it in the Properties window here so that you could see how the Properties window works.
Setting the Form’s Text Property
Notice that the text that appears in the form’s title bar says Form1. Visual Basic 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 on the title bar is determined by the value of the form’s Text property. 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. If you’re lucky, Visual Studio will have already selected this property for you.
Change the text to Picture Viewer. Press the Enter key or Tab key, or click a different property to commit your edit. You’ll see the text on the form’s title bar 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 two floppy disks) now to save your work. Visual Basic 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 the project is to be saved. Visual Basic 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 Basic create a solution folder, and if you do Visual Basic creates the solution file in the folder, and it creates a subfolder for the project and the actual files. On large projects, this is a handy feature. For now, it’s an unnecessary step, so uncheck the Create Directory for Solution box if it’s checked, and then click Save to save the project.
FIGURE 1.5 When saving a project, choose a name and location for the project and its files.
Giving the Form an Icon
Everyone who’s used Windows is familiar with icons—the little pictures that represent programs. Icons most commonly appear on the Start menu next to the name of their respective programs. In Visual Basic, 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 Picture Viewer.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 choose 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 Form’s Size
Next, you’ll change the form’s Width and Height properties. The Width and Height values are shown collectively under the Size property; Width appears to the left of the comma, and 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. (After you click it, it changes to a minus sign, as shown in 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 shown 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 two floppy disks.