Using the Label Class

Using the Label Class

The Label class is used to create a descriptive label that’s placed on a form. Label controls are typically associated with specific controls and are used to name controls such as text boxes that don’t have their own captions. Although the Label class has many of the same properties exposed by other controls—such as BackColor, ForeColor, and Text—the Label class is typically used as a static control. You’ll rarely program against the Label class directly, except perhaps to change the control’s caption text, as shown here:

label.Text = "Contents have been updated";

A Windows Forms label control has one unusual feature—it can never receive the input focus. Instead, it passes the focus to the next control in the tab order. For this reason, label controls associated with a text box or other control are usually placed immediately ahead of their associated control and are provided with a caption that includes an ampersand before the character that is to be the shortcut key. This enables a user to use Alt + the shortcut key to select the control associated with the label.



Part III: Programming Windows Forms