Skip to content

Label

In a ScriptoForm, the Microsoft .NET Label class is a control used to provide descriptive text for another control on the Form. For example, a Label can be used to add descriptive text for a TextBox control to inform the user about the type of data expected in the control. A Label control is usually paired with another control such as a TextBox or ComboBox. Both the control and associated Label would share a common word in the PowerShell variable name, such as “ServerName”, to easily identify them as a pair.

ScriptoForm with example Label control

In the ScriptoForm PowerShell script file, a control and its associated Label should be instantiated in the Controls region and then defined within the Forms script block. By convention, this control pair should be nested in the $GroupBoxMain GroupBox container.

The steps needed to implement a Label control with a ScriptoForm include:

  • Instantiate a Label object to be paired with another control that requires a label in the Controls region.
    Suggested name format: $LabelName | $ControlTypeName
  • Assign the required properties to the Label object in the Forms region.
  1. Instantiate a new System.Windows.Forms.TextBox and System.Windows.Forms.Label control pair:
    Controls Region
    #region Controls
    $FormMain = New-Object -TypeName System.Windows.Forms.Form
    $GroupBoxMain = New-Object -TypeName System.Windows.Forms.GroupBox
    <#
    Instantiate control objects here...
    #>
    $LabelServerName = New-Object -TypeName System.Windows.Forms.Label
    $TextBoxServerName = New-Object -TypeName System.Windows.Forms.TextBox
    $ButtonRun = New-Object -TypeName System.Windows.Forms.Button
    $ButtonClose = New-Object -TypeName System.Windows.Forms.Button
    $StatusStripMain = New-Object -TypeName System.Windows.Forms.StatusStrip
    $ToolStripStatusLabelMain = New-Object -TypeName System.Windows.Forms.ToolStripStatusLabel
    #endregion
  2. Assign properties to the pair and add them to the GroupBox object:
    Forms Region | $ShowMainForm
    #region Forms
    $ShowFormMain =
    {
    $FormWidth = 330
    $FormHeight = 260
    $FormMain.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon((Get-Process -Id $PID).Path)
    $FormMain.Text = "Title"
    $FormMain.Font = New-Object -TypeName System.Drawing.Font("MS Sans Serif",8)
    $FormMain.ClientSize = New-Object -TypeName System.Drawing.Size($FormWidth,$FormHeight)
    $FormMain.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen
    $FormMain.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle
    $FormMain.MaximizeBox = $false
    $FormMain.AcceptButton = $ButtonRun
    $FormMain.CancelButton = $ButtonClose
    $FormMain.Add_Shown($FormMain_Shown)
    $GroupBoxMain.Location = New-Object -TypeName System.Drawing.Point(10,5)
    $GroupBoxMain.Size = New-Object -TypeName System.Drawing.Size(($FormWidth - 20),($FormHeight - 80))
    $FormMain.Controls.Add($GroupBoxMain)
    <#
    Assign control properties and event handlers here and add to the GroupBox...
    #>
    $LabelServerName.Location = New-Object -TypeName System.Drawing.Point(15,15)
    $LabelServerName.AutoSize = $true
    $LabelServerName.Text = "Server Name:"
    $GroupBoxMain.Controls.Add($LabelServerName)
    $TextBoxServerName.Location = New-Object -TypeName System.Drawing.Point(15,35)
    $TextBoxServerName.Size = New-Object -TypeName System.Drawing.Size(($FormWidth - 50),20)
    $TextBoxServerName.TabIndex = 0
    $GroupBoxMain.Controls.Add($TextBoxServerName)
    $ButtonRun.Location = New-Object -TypeName System.Drawing.Point(($FormWidth - 175),($FormHeight - 60))
    $ButtonRun.Size = New-Object -TypeName System.Drawing.Size(75,25)
    $ButtonRun.TabIndex = 100
    $ButtonRun.Enabled = $false
    $ButtonRun.Text = "Run"
    $ButtonRun.Add_Click($ButtonRun_Click)
    $FormMain.Controls.Add($ButtonRun)
    $ButtonClose.Location = New-Object -TypeName System.Drawing.Point(($FormWidth - 85),($FormHeight - 60))
    $ButtonClose.Size = New-Object -TypeName System.Drawing.Size(75,25)
    $ButtonClose.TabIndex = 101
    $ButtonClose.Text = "Close"
    $ButtonClose.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
    $FormMain.Controls.Add($ButtonClose)
    $StatusStripMain.SizingGrip = $false
    $StatusStripMain.Font = New-Object -TypeName System.Drawing.Font("MS Sans Serif",8)
    [void]$StatusStripMain.Items.Add($ToolStripStatusLabelMain)
    $FormMain.Controls.Add($StatusStripMain)
    [void]$FormMain.ShowDialog()
    $FormMain.Dispose()
    }
    #endregion

Label Class (System.Windows.Forms) | Microsoft Learn