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 #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$ErrorProviderMain = New-Object -TypeName System.Windows.Forms.ErrorProvider#endregion - Assign properties to the pair, an event handler listener to the TextBox object, and add the pair 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 = "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$TextBoxServerName.CharacterCasing = [System.Windows.Forms.CharacterCasing]::Upper$TextBoxServerName.MaxLength = 15$TextBoxServerName.Add_TextChanged($TextBoxServerName_TextChanged)$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 - Create the
$TextBoxServerName_TextChangedevent handler for the TextBox object:Handlers Region $TextBoxServerName_TextChanged ={if ($TextBoxServerName.TextLength -eq 0){$ErrorProviderMain.Clear()$ButtonRun.Enabled = $false}elseif ($TextBoxServerName.Text -match "[^a-z0-9A-Z\-]"){$ErrorProviderMain.SetIconPadding($TextBoxServerName,-20)$ErrorProviderMain.SetError($TextBoxServerName,"The server name contains an invalid character.")$ButtonRun.Enabled = $false}else{$ErrorProviderMain.Clear()$ButtonRun.Enabled = $true}} - Use the Text property of TextBox object to retrive the current value:
Handlers Region | $ButtonRun_Click $ButtonRun_Click ={Invoke-FormAction -Action {try{ScheduleRestart($TextBoxServerName.Text, $DateTimePickerDate.Text)}catch{[void][System.Windows.Forms.MessageBox]::Show($PSItem.Exception.Message + "`n`nPlease contact $SUPPORT_CONTACT for technical support.","Exception",[System.Windows.Forms.MessageBoxButtons]::OK,[System.Windows.Forms.MessageBoxIcon]::Warning)}} -Reset {$TextBoxServerName.Clear()$TextBoxServerName.Focus()}}