|
70-526 MCTS
Label control
The Label control is commonly used to provide descriptive text for other controls, such as
text boxes. They can also be used to add descriptive text to a Form to provide the user with helpful
information such as run time information on the status of an application.
Labels can also be used to define shortcut keys for other controls. This is achieved because a Label
participates in the tab order of a form, but does not receive focus (the next control in the tab order
receives focus instead).
Inheritance hierarchy
System.Object
System.MarshalByRefObject
System.ComponentModel.Component
System.Windows.Forms.Control
System.Windows.Forms.Label
Useful properties
AutoEllipsis - Indicates whether the ellipsis character (...) appears at the right edge
of the Label, denoting that the Label text extends beyond the specified length of the Label. Used
when AutoSize is set to false.
AutoSize - Indicates whether the Label is automatically resized to display all text.
BorderStyle - This property determines if the Label is outlined with no visible border
(None), a plain line (FixedSingle), or a shadowed line (Fixed3D).
Enabled - If this property is set to false, the Label appears greyed out.
Image - Indicates the image that is displayed on the Label control.
ImageAlign - Indicates the alignment of the image on the Label control. Takes a value
from the ContentAlignment enumeration:
- BottomCenter - Content is vertically aligned at the bottom, and horizontally aligned at the center.
- BottomLeft - Content is vertically aligned at the bottom, and horizontally aligned on the left.
- BottomRight - Content is vertically aligned at the bottom, and horizontally aligned on the right.
- MiddleCenter - Content is vertically aligned in the middle, and horizontally aligned at the center (default).
- MiddleLeft - Content is vertically aligned in the middle, and horizontally aligned on the left.
- MiddleRight - Content is vertically aligned in the middle, and horizontally aligned on the right.
- TopCenter - Content is vertically aligned at the top, and horizontally aligned at the center.
- TopLeft - Content is vertically aligned at the top, and horizontally aligned on the left.
- TopRight - Content is vertically aligned at the top, and horizontally aligned on the right.
ImageIndex - The index value of the image displayed on the Label.
ImageList - The ImageList that contains the images to display in the Label
control.
TabIndex - Gets or sets the tab order of the control within its container. (inherited
from Control)
Text - The text that appears on the Label control.
TextAlign - Indicates the alignment of the text on the Label control. Takes a value
from the ContentAlignment enumeration (see above).
UseMnemonic - Indicates whether the control interprets an ampersand character (&)
in the control's Text property to be an access key prefix character.
Define a shortcut key
Shortcut keys are keys that, when pressed in combination with the Alt key, move the focus
to the desired control. In the case of the Label control, the next control in the tab order
receives focus instead. The first character after an ampersand (&) is specified in the
Text property of the control as the mnemonic character. When a user presses ALT+ the
mnemonic key, focus moves to the next control in the tab order. To achieve this do the following:
- Add a Label to the Form, near the control that you want to define a shortcut key for.
- Set the UseMnemonic property of the Label to true.
- Add a mnemonic character in the Text property, such as &Name. Pressing
ALT+N will act as the shortcut sequence.
- Set the TabIndex property to a value of one less than the TabIndex property
of the control that you want to define a shortcut key for.
Add a Label to a Form manually
The MSDN page has the following example showing how to add a Label to a Form.
public void CreateMyLabel()
{
// Create an instance of a Label.
Label label1 = new Label();
// Set the border to a three-dimensional border.
label1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
// Set the ImageList to use for displaying an image.
label1.ImageList = imageList1;
// Use the second image in imageList1.
label1.ImageIndex = 1;
// Align the image to the top left corner.
label1.ImageAlign = ContentAlignment.TopLeft;
// Specify that the text can display mnemonic characters.
label1.UseMnemonic = true;
// Set the text of the control and specify a mnemonic character.
label1.Text = "First &Name:";
/* Set the size of the control based on the PreferredHeight and PreferredWidth values. */
label1.Size = new Size (label1.PreferredWidth, label1.PreferredHeight);
// Add the Label control to the form.
this.Controls.Add(label1);
}
MSDN references
Label control
© Publicjoe, 2007
|