About Regions
Every ScriptoForm script file should follow the same format, facilitated with region blocks, to allow for easier code navigation and maintenance. Most code editors, such as Visual Studio Code, also provide code folding at the region block delineator. A typical ScriptoForm script consists of nine standard regions, each with it’s own specific purpose:
Examples
Section titled “Examples”<#============================================================================================================================Script: <%= $PLASTER_PARAM_Name %>Author: <%= $PLASTER_PARAM_Author %>
Notes:
============================================================================================================================#>
#region Settings<#Add global settings here...#>$SUPPORT_CONTACT = "<%= $PLASTER_PARAM_Author %>"#endregion
#region AssembliesAdd-Type -AssemblyName System.Windows.Forms#endregion
#region Appearance[System.Windows.Forms.Application]::EnableVisualStyles()#endregion
#region Controls$FormMain = New-Object -TypeName System.Windows.Forms.Form$GroupBoxMain = New-Object -TypeName System.Windows.Forms.GroupBox<#Instantiate control objects here...#>$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<#Instantiate component objects here...#>#endregion
#region Forms$ShowFormMain ={ $FormWidth = 330 $FormHeight = <%= $PLASTER_PARAM_FormHeight %>
$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... #>
$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)
<# Assign component properties and event handlers here and add to the Form... #>
[void]$FormMain.ShowDialog() $FormMain.Dispose()}#endregion
#region Functions<#Add function definitions here...#>#endregion
#region Handlers$FormMain_Shown ={ $ToolStripStatusLabelMain.Text = "Ready" $StatusStripMain.Update() $FormMain.Activate()}
<#Add event handlers here...#>
$ButtonRun_Click ={ $ToolStripStatusLabelMain.Text = "Working...please wait" $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()
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 ) }
$FormMain.Controls | ForEach-Object {$PSItem.Enabled = $true} $FormMain.ResetCursor() <# Reset controls here... #> $ToolStripStatusLabelMain.Text = "Ready" $StatusStripMain.Update()}#endregion
#region MainInvoke-Command -ScriptBlock $ShowFormMain#endregion