定义脚本时钟的sender和e参数使用$this和$_变量 为脚本块定义sender和e参数 就像C#中的lambda事件处理程序一样,您可以为脚本块定义param($sender,$e):$button.Add_MouseClick({param($sender,$e) [System.Windows.Forms.MessageBox]::Show(" $($sender.Name) `n $($e.Location)")}) 使用$this和$_变量 $this是事件的发送者,$_是事件args:$button.Add_MouseClick({ [System.Windows.Forms.MessageBox]::Show(" $($this.Name) `n $($_.Location)")})How can I correctly handle events of Windows Forms controls in PowerShell and use Sender and EventArgs?What's the equivalent of following C# code in PowerShell?button.MouseClick += (sender, e) => { MessageBox.Show($"{((Control)sender).Name} \n {e.Location}");}; 解决方案 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 clockUse $this and $_ VariablesDefine sender and e parameters for script blockLike 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)")}) 这篇关于PowerShell中的Windows窗体事件-PowerShell中的Sender和EventArgs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-20 20:00