Sams Teach Yourself Visual Basic 6 in 24 Hours

Sams Teach Yourself Visual Basic 6 in 24 Hours

By Greg Perry

Labels

Labels hold the primary text that appears on a form. Often, programmers use labels to place titles in forms and to label text boxes so that users know what to type into the text box. Visual Basic supports several other ways to put text on forms, but when you use the Label control, your code can subsequently, at runtime, change the label's text so that different messages can appear when needed. Figure 4.3 shows a Form window that contains a label used for the application's title.

04fig03.gif

Figure 4.3 A label forms the title on this form.

When you place labels on a form, you'll almost always set the Label control's Name property and type a new Caption value. In addition, you'll want to change the Font property and possibly the label's color and style. You will rarely write event procedure code for labels, so a label's overhead is fairly small and the programming effort required to manipulate labels is minimal.

Table 4.2 lists the most common Label control properties that you'll set as you work with the Label control.

Table 4.2. Common label properties.

Property Description
Alignment Determines whether the label's caption appears left-justified, centered, or right-justified within the label's boundaries.
AutoSize Enlarges the label's size properties, when True, if you assign a caption that is too large to fit in the current label's boundaries at runtime.
BackColor Specifies the label's background color. Click the BackColor palette's down arrow to see a list of colors and click System to see a list of common Windows control colors.
BackStyle Determines whether the background shows through the label or if the label covers up its background text, graphics, and color.
BorderStyle Determines whether a single-line border appears around the label.
Caption Holds the text that appears on the label.
Enabled Determines whether the label is active. Often, you'll change the Enabled property at runtime with code when a label is no longer needed.
Font Produces a Font dialog box in which you can set the caption's font name, style, and size.
ForeColor Holds the color of the label's text.
Height Holds the height of the label's outline in twips.
Left Holds the number of twips from the label's left edge to the Form window's left edge.
MousePointer Determines the shape of the mouse cursor when the user moves the mouse over the label.
TabIndex Specifies the order of the label in the focus order. Although labels cannot receive focus, they can be part of the focus order.
ToolTipText Holds the text that shows as a ToolTip at runtime.
Top Holds the number of twips from the label's top edge to the Form window's top edge.
Visible Determines whether the label appears or is hidden from the user.
Width Holds the width of the label in twips.
WordWrap Determines whether the label expands to fit whatever text appears in the caption.

Labels can present problems if they receive text that is too large for the label boundaries. Putting captions in labels seems easy until you think about the effects that can occur if the label is too large or too small to hold the text. By using certain property combinations, you can add automatically adjusting labels for whatever text the labels need to hold.

Suppose that you design a label that contains this long caption:

This label's caption is extremely long-winded, just like the author.

A label is rarely wide enough or tall enough to hold a caption this long. If you attempt to type text into a label's Caption property that is longer than what fits within the label's size properties (Left, Height, Top, and Width), one of the following things can take place, depending on how you've set up the label:

Share ThisShare This

Informit Network