How can I correctly handle events of Windows Forms controls in PowerShell and use Sender and EventArgs?
What\'s the equivalent of following
To correctly handle events of a Windows Forms control in PowerShell and take advantage of Sender and EventArgs you can use either of the following options:
sender and e parameters for script clock$this and $_ VariablesDefine sender and e parameters for script block
Like lambda event handlers in C#, you can define param($sender,$e) for the script block:
$button.Add_MouseClick({param($sender,$e)
[System.Windows.Forms.MessageBox]::Show(" $($sender.Name) `n $($e.Location)")
})
Use $this and $_ Variables
$this is the sender of the event and $_ is the event args:
$button.Add_MouseClick({
[System.Windows.Forms.MessageBox]::Show(" $($this.Name) `n $($_.Location)")
})