Labels.
Image by kalleboo via Flickr
You use labels for just what they sound like—to label other parts of your application. Labels usually are used to display text that cannot be edited by the user. Your code can change the text displayed by a label. Labels are based directly on the Control class:
Object MarshalByRefObject Component Control Label
The caption for a label is stored in the Text property. Because you can change that caption in code, labels can act a little like non-editable text boxes, displaying text and messages to the user. The TextAlign (formerly Alignment) property allows you to set the alignment of the text within the label.
Here’s another interesting aspect of labels—they cannot receive the focus (that is, become the selected target of keystrokes), but you can set up mnemonic characters for them with the UseMnemonic property; just specify a mnemonic character in their caption by preceding it with a & character. In that case, when the user presses Alt and the mnemonic character, the focus goes to the control after the label (that is, the control which the label is labeling), which lets you support keyboard navigation for the many controls that don’t support mnemonic characters. (For more information, see “Using Labels to Give Access Keys to Controls without Captions” later in this chapter.) You also can support images in labels with the Image property, or the Image and ImageList properties together.
Link labels are new in VB .NET. They’re based on the Label class, but also let you support Web-style hyperlinks to the Internet and other Windows forms. In other words, you can use a link label control for everything that you can use a label control for, and you can also make part of the text in this control a link to a Visual Basic object or Web page. Here’s the class inheritance hierarchy of this control:
Object MarshalByRefObject Component Control Label LinkLabel
Besides functioning as a full label control, you can display multiple hyperlinks in a single link label control, and use the LinkColor, VisitedLinkColor, and ActiveLinkColor properties to set the colors of the link, as you would in a Web page in a browser. The LinkArea property sets the area of the text that activates a link, and the LinkClicked event determines what happens when the link is clicked, as we’ll see.
Each hyperlink is an object of the LinkLabel.Link class and is stored in a collection called Links. You can use the Add method of the Links collection to specify the hyperlinks in a link label and use a LinkClicked event handler to handle the link when it is clicked. We’ll see how this works in detail later in this chapter.
And that’s enough overview—it’s time to start creating these controls, text boxes, rich text boxes, labels, and link labels in our Windows applications.