Displaying Static Text with the Label Control

Displaying Static Text with the Label Control

Label controls are used to display static text to the user. By static, I mean that the user can't change the text directly (but you can change the text with code). Label controls are one of the most commonly used controls, and, fortunately, they're also one of the easiest.

Labels are most often used to provide descriptive text for other controls, such as text boxes. Labels are also great for providing status-type information to a user, as well as for providing general instructions on a form.

You're going to build on the Picture Viewer project from Hour 6, "Building FormsAdvanced Techniques," for most of this hour. Although you'll be adding the controls to the interface, you won't be making them functional until you progress into the chapters on writing code.

Start by following these steps:

Open the Picture Viewer you worked on in Hour 6.

Double-click frmOptions.cs in the Solution Explorer window to display the Options form in the designer.

Add a new Label control to the form by double-clicking the Label item in the toolbox. The primary property of the Label control is the Text property, which determines the text displayed to the user. When a Label control is first added to a form, the Text property is set to the name of the controlthis isn't very useful. Set the properties of the new Label control as follows:








User Name:

Notice how the label resized automatically to fit your text. This is a new feature to Visual C# 2005. To create a multiline label, you would click in the Text property to display a drop-down arrow and then click the arrow to access a text entry box. You could then enter text and separate the lines by pressing Enter. In most cases, it's best to place label text on a single line, but it's nice to have the option.

Another interesting aspect of the label control is that a label can have an associated hot-key. A hotkey appears as an underlined character in the label's text, as in First Name:. When the user presses the Alt key in conjunction with the hot-key (Alt+F in this example), the focus is moved to the next control in the tab order after the label. To assign a hot-key to a label, you preface the hot-key in the label's Text property with an ampersand (&). For example, to create the F hot-key for the First Name label, you would enter the following into the Text property of the label: &First Name:.

 Python   SQL   Java   php   Perl 
 game development   web development   internet   *nix   graphics   hardware 
 telecommunications   C++ 
 Flash   Active Directory   Windows