Skip to content

TextBox

In a ScriptoForm, the Microsoft .NET TextBox class is a control that allows the user to provide text or numerical data to the underlying script.

ScriptoForm with example TextBox control

In the ScriptoForm PowerShell script file, a TextBox control should be instantiated in the Controls region and then defined within the Forms script block. A TextBox is usually paired with a matching Label control. If using the ScriptoForm VS Code snippets, the ScriptoForm Controls: Label & TextBox snippet can be used to instantiate this control pair and the ScriptoForm Properties: Label & TextBox snippet can be used to assign a default set of properties to them. By convention, this control pair should be nested in the $GroupBoxMain GroupBox container.

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

  • Instantiate a TextBox object and an associated Label object in the Controls region.
    Suggested name format: $LabelName | $TextBoxName
  • Assign the required and any optional properties to the TextBox object in the Forms region.
  • Assign Event Handlers to the TextBox object in the Forms region.
  • Add the control pair to a GroupBox object in the Forms region.
  • Define event handlers for the TextBox with script blocks in the Handlers region, as needed.
  • Utilize the value of the TextBox Text field with appropriate event handlers in the Handlers region, as needed.
  1. Instantiate a new System.Windows.Forms.TextBox and System.Windows.Forms.Label control pair:
    Controls Region
    $LabelEmail = New-Object -TypeName System.Windows.Forms.Label
    $TextBoxEmail = New-Object -TypeName System.Windows.Forms.TextBox
  2. Assign properties to the pair, an event handler listener to the ComboBox object, and add the pair to the GroupBox object:
    Forms Region | $ShowMainForm
    $LabelEmail.Location = New-Object -TypeName System.Drawing.Point(15,15)
    $LabelEmail.AutoSize = $true
    $LabelEmail.Text = "Name:"
    $GroupBoxMain.Controls.Add($LabelEmail)
    $TextBoxEmail.Location = New-Object -TypeName System.Drawing.Point(15,35)
    $TextBoxEmail.Size = New-Object -TypeName System.Drawing.Size(($FormWidth - 50),20)
    $TextBoxEmail.TabIndex = 0
    $TextBoxEmail.Add_TextChanged($TextBoxEmail_TextChanged)
    $TextBoxEmail.Add_GotFocus($TextBoxEmail_GotFocus)
    $GroupBoxMain.Controls.Add($TextBoxEmail)
  3. Set the CharacterCasing property on the TextBox object:
    Forms Region | $ShowMainForm
    $TextBoxEmail.CharacterCasing = [System.Windows.Forms.CharacterCasing]::Lower
  4. Set the MaxLength property on the TextBox object:
    Forms Region | $ShowMainForm
    $TextBoxEmail.MaxLength = 25
  5. Create the $TextBoxEmail_TextChanged event handler for the TextBox object:
    Handlers Region
    $TextBoxEmail_TextChanged =
    {
    if ($TextBoxName.TextLength -eq 0)
    {
    $ErrorProviderMain.Clear()
    $ButtonRun.Enabled = $false
    }
    else
    {
    $ErrorProviderMain.Clear()
    $ButtonRun.Enabled = $true
    }
    }
  6. Create the $TextBoxEmail_GotFocus event handler for the TextBox object:
    Handlers Region
    $TextBoxEmail_GotFocus =
    {
    if ($TextBoxEmail.TextLength -ne 0)
    {
    try
    {
    $EmailDomainName = (New-Object -TypeName MailAddress($TextBoxEmail.Text)).Host
    $TextBoxEmail.SelectionStart = 0
    $TextBoxEmail.SelectionLength = ($TextBoxEmail.TextLength - ($EmailDomainName.Length + 1))
    }
    catch {$TextBoxEmail.SelectionStart = 0}
    }
    }
  7. Use the Text property of TextBox object to retrive the current value:
    Handlers Region
    SendEmail($TextBoxEmail.Text)

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