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.

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
Textfield with appropriate event handlers in the Handlers region, as needed.
Examples
Section titled “Examples”- Instantiate a new
System.Windows.Forms.TextBoxandSystem.Windows.Forms.Labelcontrol pair:Controls Region $LabelEmail = New-Object -TypeName System.Windows.Forms.Label$TextBoxEmail = New-Object -TypeName System.Windows.Forms.TextBox - 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) - Set the
CharacterCasingproperty on the TextBox object:Forms Region | $ShowMainForm $TextBoxEmail.CharacterCasing = [System.Windows.Forms.CharacterCasing]::Lower - Set the
MaxLengthproperty on the TextBox object:Forms Region | $ShowMainForm $TextBoxEmail.MaxLength = 25 - Create the
$TextBoxEmail_TextChangedevent 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}} - Create the
$TextBoxEmail_GotFocusevent 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}}} - Use the
Textproperty of TextBox object to retrive the current value:Handlers Region SendEmail($TextBoxEmail.Text)