Home > Articles > Programming > Windows Programming

  • Print
  • + Share This
From the author of

Changing the Characteristics of Objects

Almost everything you work with in Visual Basic 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 (Hour 3, "Understanding Objects and Collections," discusses objects in detail). Objects, in turn, are classified by type. For instance, a form is a Form object, whereas items you can place on a form are called Control objects, or controls. Some objects don't have a physical appearance, but exist only in code. You'll learn about these kinds of objects in later hours.

New Term

Every object, regardless of whether it has a physical appearance, has a distinct set of attributes known as properties. You have certain properties about you, such as your height and hair color, and Visual Basic 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 in the way you desire. To display the properties of an object, click the object in its designer. Because the only object that currently exists in your new project is the default form, the form is already selected and its properties are displayed in the Properties window.

Naming Objects

The property you should set first for any new object is the Name property. Press F4 to display the Properties window (if it's not already visible), and notice the Name given to your default form (the first property listed in the Properties window)—Form1. When you first create an object, Visual Basic gives the object a unique, generic name based on the object's type. Although these names are functional, they aren't very descriptive. For instance, Visual Basic named your form Form1, but it's common to have dozens of forms in a project, and it would be extremely difficult to manage a complicated project if all forms were distinguishable only by a number (Form2, Form3, and so forth).

NOTE

In actuality, what you're creating 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, "Managing Projects," for more information.

To better manage your forms, you should give each one a descriptive name. Visual Basic gives you the chance to name new forms as they're created. Because Visual Basic created this default form for you, you didn't get a chance to name it, so you must change the filename of the form. Change the name of the form now by clicking the Name property and changing the text from Form1 to fclsViewer.

NOTE

I use the fcls prefix here to denote that the file is a form class. There are different types of classes, so using a prefix helps differentiate the classes in code. You're not required by Visual Basic to use object prefixes, but I highly recommended that you do so. In Hour 12, "Using Constants, Data Types, Variables, and Arrays," you'll learn the benefits of using a naming convention as well as the standard prefixes for many .NET 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 Basic sets the form's title bar text to the name of the form when it is 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. Use the scrollbar to locate the Text property in the Properties window and then change the text to Picture Viewer.

The Text property was titled Caption in earlier versions of Visual Basic.

Giving the Form an Icon

Everyone who has used Windows is familiar with icons, which are the little pictures used to represent programs. Icons most commonly appear in 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.

NOTE

The instructions that follow assume you have access to the source files for the examples in this book. They are available at http://www.samspublishing.com/detail_sams.cfm?item=0672320800. 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, 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. To locate the HourOne.ico file or another icon file of your choice, use the Open dialog box that appears. 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 that's displayed on the Windows taskbar.

Changing the Size of the Form

The Properties window currently shows all properties for the form in alphabetical order. Although this is useful, you may prefer to view the properties by category, particularly when you're first learning Visual Basic. Right above the list of properties in the Properties window is a group of tool buttons. Clicking the first button on the left changes the property display to categorical. The button next to this changes the display back to alphabetical.

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 by changing the corresponding number in the Size property. Both values represent the number of pixels of the dimension. To display and adjust the Width and Height properties separately, click the small plus sign (+) next to the Size property (see Figure 1.4).

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

Change the Width property to 400 and the Height to 325. To commit a property change, press Tab or click a different property or window. Your screen should now look like the one in Figure 1.5.

Figure 1.5 A change in the Properties window is reflected as soon as the change is committed.

When you first created this project, Visual Basic saved a copy of the source files in their initial state. The changes you've made so far exist only in memory; if you were to turn your computer off at this time (don't do this), you would lose any and all work up to this point. You should get into the habit of saving your work frequently. Save the project now by choosing Save All from the File menu or by clicking the Save All button on the toolbar (it has a picture of stacked disks on it).

Adding Controls to a Form

Now that your form has its properties set, you need to add objects to the form to produce a user interface. Objects that can be placed on a form are called controls. Some controls have a visible interface with which a user can interact, whereas others are always invisible to the user. You'll use controls of both types in this example. On the left side of the screen is a tab titled Toolbox. Click the Toolbox tab now to display the Toolbox window shown in Figure 1.6. The toolbox contains all the controls available in the project, such as labels and text boxes.

Figure 1.6 The toolbox is used to select controls to build a user interface.

NOTE

There are three ways to add a control to a form, and Hour 5, "Managing Projects," explains them in detail. In this hour, you'll use the technique of double-clicking a tool in the toolbox.

Refer to Hour 2, "Navigating Visual Basic," for more information on customizing the design environment.

Your Picture Viewer interface will consist of the following controls:

  • Two Button controls

  • A PictureBox control

  • An OpenFileDialog control

  • + Share This
  • 🔖 Save To Your Account