CheckBox
In a ScriptoForm, the Microsoft .NET CheckBox class is a control used to invoke an action with an Event Handler or control future actions of the script. For example, the user can check a CheckBox to trigger the
CheckedChanged event to perform an immediate action or use the state of the Checked property to influence an action later in the life cycle of the script such as filtering displayed results or limiting the scope of the action.
In a ScriptoForm PowerShell script file, a CheckBox control should be instantiated in the Controls region and then defined within the Forms script block. A CheckBox provides a built-in
Text property that is used to display a descriptive label to the right of it on the Form - no separate Label control is required. If using the ScriptoForm VS Code snippets, the ScriptoForm Controls: CheckBox snippet can be used to instantiate this control and the ScriptoForm Properties: CheckBox snippet can be used to assign a default set of properties to it. By convention, this control is typically nested in the $GroupBoxMain GroupBox container but there may be times where you need to locate it outside this container and adjacent to the Run and Close Buttons on the Form.The steps needed to implement a CheckBox control with a ScriptoForm include:
- Instantiate a CheckBox object in the Controls region.
Suggested name format: $CheckBoxName - Assign the required and any optional properties to the CheckBox object in the Forms region.
- Assign Event Handlers to the CheckBox object in the Forms region, as needed.
- Add the control to a GroupBox object in the Forms region.
- Define event handlers for the CheckBox with script blocks in the Handlers region, as needed.
- Utilize the value of the CheckBox
Checkedfield with appropriate event handlers in the Handlers region, as needed.
Examples
Section titled “Examples”- Instantiate a new
System.Windows.Forms.CheckBoxcontrol:Controls Region #region Controls$FormMain = New-Object -TypeName System.Windows.Forms.Form$GroupBoxMain = New-Object -TypeName System.Windows.Forms.GroupBox<#Instantiate control objects here...#>$CheckBoxOutdated = New-Object -TypeName System.Windows.Forms.CheckBox$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#endregion - Assign properties and event handler to the control and add it 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...#>$CheckBoxOutdated.Location = New-Object -TypeName System.Drawing.Point(15,($FormHeight - 115))$CheckBoxOutdated.AutoSize = $true$CheckBoxOutdated.TabIndex = 1$CheckBoxOutdated.Text = "Outdated Applications"$CheckBoxOutdated.Add_CheckedChanged($CheckBoxOutdated_CheckedChanged)$GroupBoxMain.Controls.Add($CheckBoxOutdated)$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 event handler for the CheckBox object:
Handlers Region | $CheckBoxOutdated_CheckedChanged $CheckBoxOutdated_CheckedChanged ={Invoke-FormAction -Action {try {Get-WingetData -UpdatesOnly:$CheckBoxOutdated.Checked}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)}}}