Share via


Visual Basic Concepts

Using the Label Control

Label controls are used to display text and cannot be edited by the user. They are used to identify objects on a form — provide a description of what a certain control will do if clicked, for example — or at run time, they can display information in response to an event or process in your application.

Figure 7.29   The label control

Labels are used in many instances, for many different purposes. Most commonly, they are used to label controls that don't have their own Caption properties. For example, you can use the label control to add descriptive labels to text boxes, list boxes, combo boxes and so on. They can also be used to add descriptive text to a form, for example, to provide the user with Help information.

You can also write code that changes the text displayed by a label control in response to events at run time. For example, if your application takes a few minutes to process a change, you can display a processing-status message in a label.

Because the label control cannot receive the focus, it can also be used to create access keys for other controls.

Setting the Label's Caption

To change the text displayed in the label control, use the Caption property. At design time, you can set this property by selecting it from the control's Properties window.

You can set the length of the Caption property up to a maximum of 1024 bytes.

Aligning Text

The Alignment property allows you to set the alignment of the text within the label control to either Left Justify (0, the default) , Center (1), or Right Justify (2).

The AutoSize and WordWrap Properties

By default, when text entered into the Caption property exceeds the width of the control, the text wraps to the next line and is clipped if it exceeds the control's height.

To allow the control to automatically adjust to the size of its contents, set the AutoSize property to True. The control will expand horizontally to fit the entire contents of the Caption property. To allow the contents to wrap down and expand vertically, set the WordWrap property to True.

For More Information   See "Using Labels to Display Text" in "Forms, Controls, and Menus" for a simple demonstration of the AutoSize and WordWrap properties.

Using Labels to Create Access Keys

Set the UseMnemonic property to True if you want to define a character in the Caption property of the label as an access key. When you define an access key in a label control, the user can press and hold down ALT+ the character you designate to move the focus to the next control in the tab order.

You can also create access keys for any other controls that have a Caption property by adding an ampersand (&) before the letter you want to use as the access key. To assign an access key to controls that don't have captions, use a label with the control. Because labels can't receive focus, focus automatically moves to the next control in the tab order. Use this technique to assign access keys to text boxes, picture boxes, combo boxes, list boxes, drive list boxes, directory list boxes, grids, and images.

To assign an access key to a control with a label

  1. Draw the label first, and then draw the control.

    -or-

    Draw the controls in any order and set the TabIndex property of the label to one less than the control.

  2. Use an ampersand in the label's Caption property to assign the access key for the label.

    Note   You may want to display ampersands in a label control, rather than using them to create access keys. This may occur if you bind a label control to a field in a recordset where the data includes ampersands. To display ampersands in a label control, set the UseMnemonic property to False.