Form Action Helper Function
When performing an action with a ScriptoForm, typically with an Event Handler, it is generally best practice for usablitity purposes to set the state of the Form and Controls before, during, and after the processing of that action. This may include:
- Disabling controls to prevent user interaction
- Setting status messages
- Setting the mouse cursor
- Re-enabling controls
- Clearing control values
- Setting focus to a specific control
To facilitate these state changes for most common scenarios, the Invoke-FormAction function is provided that can be called from your Event Handler.
function Invoke-FormAction{ param ( [Parameter(Mandatory, Position = 0)] [ScriptBlock]$Action, [Parameter(Position = 1)] [ScriptBlock]$Reset = $null, [Parameter(Position = 2)] [String]$StatusText = "Working...please wait" )
try { $ToolStripStatusLabelMain.Text = $StatusText $FormMain.Controls | Where-Object {$PSItem -isnot [System.Windows.Forms.StatusStrip]} | ForEach-Object {$PSItem.Enabled = $false} $FormMain.Cursor = [System.Windows.Forms.Cursors]::WaitCursor [System.Windows.Forms.Application]::DoEvents() Invoke-Command -ScriptBlock $Action }
finally { $FormMain.Controls | ForEach-Object {$PSItem.Enabled = $true} $FormMain.ResetCursor() if ($Reset) {Invoke-Command -ScriptBlock $Reset} $ToolStripStatusLabelMain.Text = "Ready" $StatusStripMain.Update() }}The overall flow of this function is as follows:
- The
$ToolStripStatusLabelMain.Textvalue is set to a default value of:Working...please wait. This value can be adjusted with theStatusTextparameter if needed. This text provides visible feedback to the user that the action steps have begun. - All controls on the Form, with the exception of the
$StatusStripMaincontrol, are disabled. This prevents the user from interacting with the Form while the action steps are running. The$StatusStripMaincontrol is left enabled to allow status messages via the$ToolStripStatusLabelMain.Textvalue to be updated as needed while the action steps are running. - The Form mouse cursor is set to the
WaitCursorvalue. This provides visiable feedback to the user that the action steps are running. - The action steps are executed via the
Actionparameter script block. - The action steps finish and exceptions are handled if needed.
- All Form controls are enabled.
- The Form mouse cursor is reset to the default value.
- The Form controls are reset to a desired state via the
Resetparameter script block. For example, anInputcontrol text value can be cleared. Additionally, focus to a specific control can be set via this script block as well. This, in effect, resets the Form to it’s original state. - The
$ToolStripStatusLabelMain.Textvalue of the Form is reset to the default value ofReady.
Example
Section titled “Example”$ButtonRun_Click ={ Invoke-FormAction -Action { try { Start-Sleep -Seconds 2 [void][System.Windows.Forms.MessageBox]::Show( "Action complete.", "Results", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Information ) } 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() }}