How to Add a PowerShell GUI Event Handler with Parameters (Part 2)
Introduction
This a continuation of the previous article "How to add a PowerShell GUI event handler ", this article will demonstrate how to add an event handler to a PowerShell GUI application that uses parameters; This can done in two ways; embedded code and a function;
The example to demonstrate the 2 ways of adding an event handler with parameters, is an explorer application that consists of a form with a treeview and a listview that shows the WIN32_Classes (objects returned by the Get-WMIObject cmdlet).
Complete PowerShell source code is available in the gallery PowerShell GUI Application - WIN 32 Class Explorer.
The statement that will be examined;
$TreeView.Add_AfterSelect()
What goes in the the parentheses determines how the event handler is called/handled.
The treeview event handler definition is:
void TreeView1_AfterSelect(System.Object sender, System.Windows.Forms.TreeViewEventArgs e)
The parameter sender is the treeview that generated the event, in this case of the sample application, there is only treeview present on the form and will be the treeview that always generates the event.
The parameter e is an object with several properties. The node property contains the node that triggered the AfterSelect event.
Method 1 - Embedded Code
This method puts the event handler inside the parentheses of the $TreeView.Add_AfterSelect() call. This code sample is only snippet, the entire code can be downloaded from the gallery PowerShell GUI Application - WIN 32 Class Explorer. 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 = "WIN 32 Class Explorer"
$Form.Size = New-Object System.Drawing.Size(200,200)
$Form.StartPosition = "CenterScreen"
$Form.Topmost = $True
....
# Add TreeView
$TreeView = New-Object System.Windows.Forms.TreeView
$Form.Controls.Add($TreeView)
#Add Button event
$TreeView.Add_AfterSelect(
{
$TreeView.SelectedNode = $_.Node
# Determine node type and procese node
}
)
#Show the Form
$Form.ShowDialog()| Out-Null
} #End Function
#Call the Function
Generate-Form
Method 2 - Function
This method calls an event handler that is a PowerShell function. The function is called from within $TreeView.Add_AfterSelect() call and the function must be defined to accept the parameters. This code sample is only snippet, the entire code can be downloaded from the gallery PowerShell GUI Application - WIN 32 Class Explorer. Relevant parts are in bold.
Function TreeView_AfterSelect
{
[CmdletBinding()]
param (
[parameter(Mandatory=$True)]
[Object]$Sender,
[parameter(Mandatory=$True)]
[Object]$E
)
Try
{
$Sender.SelectedNode = $E.Node
# Determine node type and procese node
}
Catch [System.Exception]
{
Write-Verbose $_.Exception.Message
}
}
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 = "WIN 32 Class Explorer"
$Form.Size = New-Object System.Drawing.Size(200,200)
$Form.StartPosition = "CenterScreen"
$Form.Topmost = $True
....
# Add TreeView
$TreeView = New-Object System.Windows.Forms.TreeView
$Form.Controls.Add($TreeView)
#Add Button event
$TreeView.Add_AfterSelect(
{
TreeView_AfterSelect -Sender $TreeView -EventArg $_
}
)
#Show the Form
$form.ShowDialog()| Out-Null
} #End Function
#Call the Function
Generate-Form