How to Add a PowerShell GUI Event Handler (Part 1)
Introduction
There are 3 ways to add an event handler to a Powershell GUI application; using embedded code, a variable and a function
The example to demonstrate the 3 ways, consists of a form and a button. When the button is clicked, it calls the click event handler for the button and displays a dialog box with message "Hello world."
https://kenugg.by3301.livefilestore.com/y2pVo58jvhzXxgG5MH3xNG9X3tPc_bURMRKk6L6vzaFbLx5rhGUWviYQYd3FRbVOp-n_HH0jNkA3iWgE7xTJZTUf2T6iUmiC-3Jka2xAACg-HA/Event-Handler-HelloWorld1.png?psid=1 https://kenugg.by3301.livefilestore.com/y2pRkiZUmZU-__AoNJrwaQCLu6qZ_txjgs_gB-CIBrYPQCwfnTftIgtIIM8hESsp9HUMNId9B6PxEM2P1iAasUBRBZergfazdII_GurchIXEvg/Event-Handler-HelloWorld2.png?psid=1
This statement is required for all three methods;
$Button.Add_Click()
What goes in the the parentheses determines how the event handler is called/handled.
Method 1 - Embedded Code
This method puts the event handler inside the parentheses of the $Button.Add_Click call. This method works, but from coding point of view it is not the preferred way, as it make it difficult to find, as the number of controls on the form increases and the complexity grows. Relevant parts are in bold.
Function Generate-Form {
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
# Build Form
$Form = New-Object System.Windows.Forms.Form
$Form.Text = "My Form"
$Form.Size = New-Object System.Drawing.Size(200,200)
$Form.StartPosition = "CenterScreen"
$Form.Topmost = $True
# Add Button
$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size(35,35)
$Button.Size = New-Object System.Drawing.Size(120,23)
$Button.Text = "Show Dialog Box"
$Form.Controls.Add($Button)
#Add Button event
$Button.Add_Click(
{
[System.Windows.Forms.MessageBox]::Show("Hello World." , "My Dialog Box")
}
)
#Show the Form
$form.ShowDialog()| Out-Null
} #End Function
#Call the Function
Generate-Form>
Method 2 - Variable
This method calls an event handler that is a PowerShell variable. From a coding point of view it is better, more consistent with other GUI languages. Relevant parts are in bold.
$Button_Click =
{
[System.Windows.Forms.MessageBox]::Show("Hello World." , "My Dialog Box")
}
Function Generate-Form {
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
# Build Form
$Form = New-Object System.Windows.Forms.Form
$Form.Text = "My Form"
$Form.Size = New-Object System.Drawing.Size(200,200)
$Form.StartPosition = "CenterScreen"
$Form.Topmost = $True
# Add Button
$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size(35,35)
$Button.Size = New-Object System.Drawing.Size(120,23)
$Button.Text = "Show Dialog Box"
$Form.Controls.Add($Button)
#Add Button event
$Button.Add_Click($Button_Click)
#Show the Form
$form.ShowDialog()| Out-Null
} #End Function
#Call the Function
Generate-Form
Method 3 - Function
This method calls an event handler that is a PowerShell function. From a coding point of view it is even better, consistent with other GUI languages, and is the only way if there are parameters for the event handler. Relevant parts are in bold.
Function Button_Click()
{
[System.Windows.Forms.MessageBox]::Show("Hello World." , "My Dialog Box")
}
Function Generate-Form {
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
# Build Form
$Form = New-Object System.Windows.Forms.Form
$Form.Text = "My Form"
$Form.Size = New-Object System.Drawing.Size(200,200)
$Form.StartPosition = "CenterScreen"
$Form.Topmost = $True
# Add Button
$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size(35,35)
$Button.Size = New-Object System.Drawing.Size(120,23)
$Button.Text = "Show Dialog Box"
$Form.Controls.Add($Button)
#Add Button event
$Button.Add_Click({Button_Click})
#Show the Form
$form.ShowDialog()| Out-Null
} #End Function
#Call the Function
Generate-Form