Label
Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio
| Exam 70-526 Preparation Guide: Create and configure text display controls on a Windows Form |
Contents |
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 theLabel, denoting that theLabeltext extends beyond the specified length of theLabel. Used whenAutoSizeis set tofalse.
-
-
AutoSize- Indicates whether theLabelis automatically resized to display all text.
-
-
BorderStyle- This property determines if theLabelis outlined with no visible border (None), a plain line (FixedSingle), or a shadowed line (Fixed3D).
-
-
Enabled- If this property is set tofalse, theLabelappears greyed out.
-
-
Image- Indicates the image that is displayed on theLabelcontrol.
-
-
ImageAlign- Indicates the alignment of the image on theLabelcontrol. Takes a value from theContentAlignmentenumeration:
-
-
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 theLabelcontrol.
-
-
ImageList- The ImageList that contains the images to display in theLabelcontrol.
-
-
TabIndex- Gets or sets the tab order of the control within its container. (inherited fromControl)
-
-
Text- The text that appears on theLabelcontrol.
-
-
TextAlign- Indicates the alignment of the text on theLabelcontrol. Takes a value from theContentAlignmentenumeration (see above).
-
-
UseMnemonic- Indicates whether the control interprets an ampersand character (&) in the control'sTextproperty 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
Labelto the Form, near the control that you want to define a shortcut key for. - Set the
UseMnemonicproperty of theLabeltotrue. - Add a mnemonic character in the
Textproperty, such as&Name. Pressing ALT+N will act as the shortcut sequence. - Set the
TabIndexproperty to a value of one less than theTabIndexproperty of the control that you want to define a shortcut key for.
Add a Label to a Form manually
The MSDN page for the Label control 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
|

