The Label Control
- Details
- Written by June Blender
- Last Updated: 01 September 2016
- Created: 16 December 2015
- Hits: 30431
Label Control [System.Windows.Forms.Label]
Creates a clickable text box in a GUI application. Use a label to display text strings.
- MSDN Page: System.Windows.Forms.Label
- Default Event: Click
Tip: To display multiple lines of text, use a Textbox or RichTextBox control.
Important properties of the Label object
Important Events:
Important properties of the Label object
Text property: Determines the text in the label
Use the Text property to set, get, or change the text that is displayed on the label.
Value type: System.String
Default value: (None)
To set the initial value of Text property, that is, the value that appears when the GUI app starts, use the Properties pane. On the Text row, type the text that appears on the Label.
NOTE: When you change the value of the Text property of a Label in the Properties pane, PowerShell Studio changes the name of the variable that stores the Label object in your script. For example, if you change the Text property value to ‘Close’, PowerShell Studio changes all references of the Label variable to $LabelClose.
To get or change the label text while the GUI app is running, use the Text property in the event handlers in your script.
To get the text in the label:
$LabelMain.Text
|
To set the text in the label use an assignment statement. You can include variables and expressions in the assigned string value.
$Name = 'PowerShell' $LabelMain.Text = "Hello, $Name" |
To change the text in the label:
$LabelMain_click = { # Get the text currently in the label and evaluate it. If ($LabelMain.Text -eq 'Click') { $LabelMain.Text = 'Clicked' # Change the text in the label. } Else { $LabelMain.Text = 'Click' } } |
Tip: To direct Windows PowerShell to evaluate an expression before inserting the result in a string, use a subexpression. The syntax is $( <expression ).
For example:
Without subexpression:
$LabelMain.Text = "Building automation on (Get-Process 'PowerShell Studio').MainWindowTitle" |
With subexpression:
$LabelMain.Text = "Building automation on $((Get-Process 'PowerShell Studio').MainWindowTitle)" |
Font property: Determines the style and size of the label text
Use the Font property to get, set, or change the font of the label text.
Value Type: System.Drawing.Font
Default: Microsoft Sans Serif 8.25.
To set the font easily, in the Properties pane for the Label, in the Font row, click the ellipsis (…) and then use the Font selector.
To display the properties of the Font object, in the Properties pane, click the Font group arrow.
To script a font change, set the Font property to a string with the Font name, size, and style.
$labelMain.Font = 'Segoe UI, 15.75pt, style=Bold, Italic' |
To determine other properties of the font, create a System.Drawing.Font object.
$ClickFont = [System.Drawing.Font]::new('Microsoft Sans Serif', 8.25, [System.Drawing.FontStyle]::Regular) $ClickedFont = [System.Drawing.Font]::new('Segoe UI', 16, [System.Drawing.FontStyle]::Italic) $labelMain_Click={ if ($labelMain.Text -eq 'Click') { $labelMain.Text = 'Clicked' $labelMain.Font = $ClickedFont } else { $labelMain.Text = 'Click' $labelMain.Font = $ClickFont } } |
Tips:
- To enlarge the label automatically to fit its contents, set the AutoSize property of the Label to $True.
- When scripting a font, be sure to set the Font property of the Label. The Text property of the Label does not have a Font property.
ForeColor property: Determines the color of the label text.
Use the ForeColor property to get, set, and change the color of the text in the label.
Values: Any [System.Drawing.Color] type
Default: ControlText (Black)
To set the ForeColor property in the Properties pane.
To set ForeColor property in a script:
$Label1.ForeColor = [System.Drawing.Color]::Red |
-or-
$Label1.ForeColor = "Red" |
BackColor property: Determines the color of the background in the label
Use the BackColor property to get, set, and change the color of the label background.
Value Type: [System.Drawing.Color]
Default: (Determined by the operating system. Typically ‘Control’)
To set the BackColor property in a script:
If ($Label1.BackColor -eq 'Control') { $Label1.BackColor = [System.Drawing.Color]::ControlDark } |
-or-
$Label1.BackColor = "ControlDark"
TextAlign property: Determines the position of the text in the label
Use the TextAlign property to get, set, or change the alignment of the text relative to the label border.
Value Type: [System.Drawing.ContentAlignment] enumeration
Values: TopLeft, TopCenter, TopRight, MiddleLeft, MiddleCenter, MiddleRight, BottomLeft, BottomCenter, BottomRight
Default: MiddleCenter
To set the TextAlign property in the Property pane:
To set the TextAlign property in a script:
$label1.TextAlign = 'TopRight' |
BorderStyle property: Determines the style and thickness of the label border.
Use the BorderStyle property to get, set, or change the label border. Label borders are fixed. The end-user cannot resize any label border style.
Value Type: [System.Windows.Forms.BorderStyle] enumerator.
Values: None, FixedSingle, Fixed3D
Default: None
$label1.BorderStyle = 'None' |
-or-
$label1.BorderStyle = [System.Windows.Forms.BorderStyle]'None' |
$label1.BorderStyle = 'FixedSingle' |
$label1.BorderStyle = 'Fixed3D' |
Image property: Determines that image that appears on the label
Use this property to display an icon, bitmap, or image of any type on the label.
Value Type: [System.Drawing.Image]
Default: (None; Same as Form.BackgroundImage)
To add an image from a file to a label, use Image property in the Property pane. Click the ellipsis and, in File Explorer, select a file. PowerShell Studio converts the file into a bitmap string (64-bit byte array) so it is independent of the local system.
On the label:
Tip: If you are using a label to display an image without text, consider using a PictureBox object.
Note: To add an image to a label, PowerShell Studio converts the image to a 64-bit string and then uses the FromBase64String static method to pass a byte array as the System.Drawing.Image type. It does not pass a path or use any local system values.
Enabled property: Determines whether the label can be clicked
Use the Enabled property to temporarily enable or disable the click feature of the label. For example, use Enabled = $false to disable the click feature after the label has been clicked or while a function is running.
Value Type: [System.Boolean] ($True, $False).
Default: $True
To completely disable the click feature of the label, omit the Click event handler or enter an empty script block.
To set the Enabled property in the script:
$Label1.Enabled = $false |
For example:
If ($Label1.Text -eq 'Click') { $Label1.Text -eq 'Clicked' $Label1.Enabled = $false } |
AutoSize property: Adjusts the label length to enclose its contents
The AutoSize property automatically adjusts the length of the label (horizontally) to enclose the string in the value of its Text property.
Use an AutoSize value of $True when the contents of the label are not predetermined, such as when the label displays a value that is returned at runtime or the label can display text in multiple languages. To resize the label or set a fixed size, use $False.
Value Type: [System.Boolean] ($True, $False).
Default: $True (PowerShell Studio 5.2.124 and later. Otherwise, $False)
When the value of AutoSize is $True, Windows automatically resizes the label at runtime to display the text within the label borders.
The size of the label in the designer might not indicate its actual size when the form is displayed.
When the value of AutoSize is $False, you determine the label size, which is fixed, regardless of its content. To resize the label, the value of AutoSize must be $False.
AutoSize extends only the length of the label (horizontally) and the Label control does not have a MultiLine property. If the text exceeds the length of the form, the label and its text are truncated.
To use an ellipsis to prevent truncated text, set the value of AutoEllipsis to $True and AutoSize to $False.
To wrap the text in a Label to the next line, set AutoSize to $False and then increase the height of the label (vertically).
Important Events:
Click event: Determines how the label responds when a user clicks it.
The event handler for the Click event is called when the label is clicked.
IMPORTANT: The Click event is included because it's the default event for the Label control. However, because the Label is static, not obviously clickable, and provides no user feedback when clicked, best practice guidelines recommend using a Button or LinkLabel control for most clicking actions.
$label1_Click={ $label1.Enabled = $false $label1.Text = "Disabled" } |
Before Click event:
After Click event:
Note about events:
To discover which control called the event script block, use the $this automatic variable. The $this variable is particularly useful when you have multiple controls calling the same event script block. The $this variable tells which control triggered the event, so you can respond in a control-specific way.
For example, this Click event script block disables the control that called it:
$Disable_Click={ $this.Enabled = $false } |
Looking for more Spotlight articles? In PowerShell Studio, in the Toolbox or in the Properties pane, right-click a control and then click ‘View Spotlight Article’.
June Blender is a technology evangelist at SAPIEN Technologies, Inc. You can reach her at This email address is being protected from spambots. You need JavaScript enabled to view it. or follow her on Twitter at @juneb_get_help.
For licensed customers, use the forum associated with your product in our Product Support Forums for Registered Customers.
For users of trial versions, please post in our Former and Future Customers - Questions forum.