Controls for Displaying Text

The Label control Label control can be used by the programmer for displaying text on the form. This control should not be confused with the Textbox control, which is used by the user for entering text into data fields.

Reviewing the Label Control's Properties

Clicking on the Properties icon will display the properties dialog box, which by default shows the General tab properties.

Label Properties

Using Labels to Display Text

An ALabel control displays text that the user cannot directly change. You can use labels to identify controls, such as ATextBox and AScrollbar controls, that do not have their own Caption property. The actual text displayed in a label is set by the Caption property, which can be set at design time in the Properties window or at run time by assigning it in code.

By default, the caption is the only visible part of the label control. However, if you set the BorderStyle property to 1 (which you can do at design time), the label appears with a border — giving it a look similar to a text box. You can also change the appearance of the label by setting the BackColor, BorderStyle, ForeColor, and Font properties.

Sample

Example of form with Label caption

In this sample, frmLabel is the form variable for a form called LabelForm.

load frmLabel

lbl.Alignment = 2 ! Align text to center

lbl.Font.Name = "Arial Black"

lbl.Font.Size = 16

lbl.Caption = "My Caption"

Sizing a Label to Fit Its Contents

Single-line label captions can be specified at design time in the Properties window. If you want to enter a longer caption, or a caption that will change at run time, labels have two properties that help you size the controls to fit larger or smaller captions: AutoSize and WordWrap.

The AutoSize property determines if a control should be automatically resized to fit its contents. Here is an ALabel control whose Caption property will enlarge by several words when clicked.

lbl.Caption = "History of Cheese"

lbl.AutoSize = 1 ! Set AutoSize to True

lbl.WordWrap = 1 ! Set WordWrap to True

.

.

Procedure lbl.OnClick() : VCoid Public

{

lbl.Caption = "History of Cheese With Attention To Roquefort."

}

The control is now in its original state, prior to clicking.

Form without WordWrap or AutoSize

If AutoSize is set to True, the ALabel control grows horizontally to fit its contents when its contents are enlarged, as shown in the following image.

Form with AutoSize Turned On

The WordWrap property causes the ALabel control to grow vertically to fit its contents, while retaining the same width, as shown in the following image.

AutoSize and WordWrap turned on

Note: In order for the ALabel control's WordWrap property to take effect, AutoSize must be set to True. The width of the label is increased only if the width of a single word exceeds the current width of the control.