Windows Forms Events in PowerShell - Sender and EventArgs in PowerShell

前端 未结 1 803
深忆病人
深忆病人 2021-01-14 02:00

How can I correctly handle events of Windows Forms controls in PowerShell and use Sender and EventArgs?

What\'s the equivalent of following

相关标签:
1条回答
  • 2021-01-14 02:39

    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:

    • Define sender and e parameters for script clock
    • Use $this and $_ Variables

    Define 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)")
    })
    
    0 讨论(0)
提交回复
热议问题