Skip to content

Handlers

The Handlers region is used to define all form, component, and control event handlers, which are script blocks that are executed when an event is raised. Examples of events include clicking a button, typing text into a textbox, and showing a form.

The region is denoted with the #region Handlers tag and occurs between the Functions and Main regions in the ScriptoForm script.

PowerShell
#region Handlers
$FormMain_Shown =
{
$ToolStripStatusLabelMain.Text = "Ready"
$StatusStripMain.Update()
$FormMain.Activate()
}
<#
Add event handlers here...
#>
$ButtonRun_Click =
{
Invoke-FormAction -Action {
try
{
<#
Do work here...
#>
}
catch
{
<#
Add custom exception handling here...
#>
[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 {
<#
Reset controls here...
#>
}
}
#endregion

The Handlers region in a newly provisioned ScriptoForm contains the following comment block:

Handlers Region
<#
Add event handlers here...
#>

This comment block should be removed (or replaced with additional event handler script blocks if needed), as it is intended only to provide an initial visual reminder.

Handlers Region | Example
#region Settings
<#
Add event handlers here...
#>
$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
}
}
#endregion

Events overview (Windows Forms .NET) | Microsoft Learn