Home > Articles > Programming > Windows Programming

  • Print
  • + Share This
This chapter is from the book

Changing the Appearance of a Form

Take a moment to browse the rest of the form's properties in the Properties window. I'll show you how to use some basic properties of the form to tailor its appearance.

TIP

To get help on any property at any time, select the property in the Properties window and press F1.

Displaying Text on a Form's Title Bar

You should always set the text in a form's title bar to something meaningful. (Note: not all forms have title bars.) The text displayed in the title bar is the value placed in the form's Text property. Generally, the text should be one of the following:

  • The name of the program.
  • The purpose of the form.
  • The name of the form.

Change the Text property of your form to Building Forms Example. Your form should now look like the one in Figure 5.1.

Figure 5.1. Use common sense when setting title bar text.

NOTE

As with most other form properties, you can change text at any time using Visual Basic code.

Changing a Form's Background Color

Although most forms appear with a gray background (this is part of the standard 3D color scheme in Windows), you can change a form's background to any color you like. To change a form's background color, you change its BackColor property. The BackColor property is a unique property in that you can specify a named color or an RGB value in the format Red, Green, Blue.

By default, the BackColor is set to the color named Control. This color is a system color, and may not be gray. When Windows is first installed, it's configured to a default color scheme. In the default scheme, the color for forms and other objects is the familiar "battleship" gray. However, as a Windows user, you're free to change any system color you desire. For instance, some people with color blindness prefer to change their system colors to colors that have more contrast than the defaults so that objects are more clearly distinguishable. When you assign a system color to a form or control, the appearance of the object adjusts itself to the current user's system color scheme. This doesn't just occur when a form is first displayed; changes to the system color scheme are immediately propagated to all objects that use the affected colors.

Change the background color of your form to blue now by deleting the word Control in the BackColor property in the Properties window, and in its place enter 0,0,255 and press Enter or Tab to commit your entry. Your form should now be blue because you entered an RGB value in which you specified no red, no green, and maximum blue (color values range from 0 to 255). In reality, you'll probably rarely enter RGB values. Instead, you'll select colors from color palettes. To view color palettes from which you can select a color for the BackColor property, click the drop-down arrow in the BackColor property in the Properties window (see Figure 5.2).

Figure 5.2. All color properties have palettes from which you can choose a color.

When the drop-down list appears, the color Blue on the Web tab is selected. This occurs because when you entered the RGB value 0,0,255, Visual Basic looked for a named color composed of the same values and it found blue. Select the System tab to see a list of the available system colors and choose Control from the list to change the BackColor of your form back to the default Windows color.

Adding an Image to a Form's Background

In addition to changing the color of a form's background, you can also place a picture on it. To add a picture to a form, set the form's BackgroundImage property. When you add an image to a form, the image is "painted" on the form's background. All the controls that you place on the form appear on top of the picture.

Add an image to your form now by following these steps:

  1. Select the form.

  2. Click the BackgroundImage property in the Properties window.

  3. Click the Build button that appears next to the property (the small button with three dots).

  4. Use the Open dialog box that appears to locate and select a GIF file from your hard drive (I used Blue Lace 16.GIF, which I found in my \WinNT folder).

Visual Basic always tiles an image specified in a BackgroundImage property (see Figure 5.3). This means that if the selected picture isn't big enough to fill the form, Visual Basic will display additional copies of the picture, creating a tiled effect. If you want to display a single copy of an image on a form, anywhere on the form, you should use a picture box.

Notice that to the left of the BackgroundImage property is a small box containing a plus sign. This indicates that there are related properties, or subproperties, of the BackgroundImage property. Click the plus sign now to expand the list of subproperties (see Figure 5.3). In the case of the BackgroundImage property, Visual Basic shows you a number of properties related to the image assigned to the property, such as its dimensions and image format.

Figure 5.3. Images are tiled to fill the form.

TIP

Adding background images to forms can add pizzazz to a program, but it can also confuse users by making forms unnecessarily busy. Try to avoid adding images just because you can. Use discretion, and add an image to a form only when the image adds value to the interface.

Removing an image from a form is just as easy as adding the image in the first place. To remove the picture that you just added to your form, right-click the BackgroundImage property name and choose Reset from the shortcut menu that appears.

NOTE

You must right-click the Name column of the property, not the Value column. If you right-click the value of the property, you get a different shortcut menu that doesn't have a Reset option.

Giving a Form an Icon

The icon assigned to a form appears in the left side of the form's title bar, in the taskbar when the form is minimized, and in the iconic list of tasks when you press Alt+Tab to switch to another application. The icon often represents the application; therefore, you should assign an icon to any form a user can minimize. If you don't assign an icon to a form, Visual Basic supplies a default icon to represent it when the form is minimized. This default icon is generic and unattractive, and you should avoid it.

You assign an icon to a form in much the same way you assign an image to the BackgroundImage property. Add an icon to your form now by clicking the form's Icon property in the Properties window, clicking the Build button that appears, and selecting an icon file from your hard drive. After you've selected the icon, it appears in the form's title bar to the left.

Run your project by pressing F5, and then click the form's Minimize button to minimize it to the taskbar. Look at the form in the taskbar; you'll see both the form's caption and the form's icon displayed (see Figure 5.4).

Figure 5.4. Assigning meaningful icons to your forms makes your application easier to use.

Stop the project now by choosing Stop Debugging from the Debug menu.

Specifying the Initial Display Position of a Form

The location on the display (monitor) where a form first appears isn't random but is controlled by the form's StartPosition property. The StartPosition property can be set to one of the values in Table 5.1.

Table 5.1 Values for the StartPosition Property

Value

Description

Manual

The Location property of the form determines where the form first appears.

CenterScreen

The form appears centered in the display.

WindowsDefaultLocation

The form appears in the Windows default location, which is toward the upper left of the display.

WindowsDefaultBounds

The form appears in the Windows default location with its bounds (size) set to the Windows default bounds.

CenterParent

The form is centered within the bounds of its parent form.


NOTE

Generally, it's best to set the StartPosition property of all your forms to CenterParent, unless you have a specific reason to do otherwise.

  • + Share This
  • 🔖 Save To Your Account