按下按钮后,winforms不执行任何操作
目标是在按下按下按钮后,winforms不执行任何操作,winforms,powershell,Winforms,Powershell,目标是在按下System.Windows.Forms.Button后更改System.Windows.Forms.Label的文本。我有下面的OOP代码。哪个()有效: 现在,我正试图将其改写为不起作用的程序风格: Add-Type -AssemblyName System.Windows.Forms Add-Type -AssemblyName System.Drawing $global:mbutton = [System.Windows.Forms.Button]::new()
System.Windows.Forms.Button
后更改System.Windows.Forms.Label的文本。我有下面的OOP代码。哪个()有效:
现在,我正试图将其改写为不起作用的程序风格:
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$global:mbutton = [System.Windows.Forms.Button]::new()
$mbutton.Text = "toggle state"
$mbutton.Location = [System.Drawing.Point]::new(100,100)
# $mbutton.Add_Click( {Button_Click} ) # pressing button shows errors on console
$global:mlabel = [System.Windows.Forms.Label]::new()
$mlabel.Name = "status"
$mlabel.Text = "enabled"
$global:Form = New-Object System.Windows.Forms.Form
$Form.Controls.Add($mlabel)
$Form.Controls.Add($mbutton)
$Form.ShowDialog() | Out-Null
$mbutton.Add_Click( {Button_Click} ) # pressing button does nothing
Function Button_Click() {
if ($Form.Parent.Controls["status"].Text -eq "enabled"){
$Form.Parent.Controls["status"].Text = "disabled"
}
else{
$Form.Parent.Controls["status"].Text = "enabled"
}
}
我做错了什么?如何调试此类问题?除非您绝对需要,否则最好不要使用全局定义。但在这种情况下,如果你稍微改变一下事情的顺序,它就会起作用:
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
Function Button_Click() {
if ($mlabel.Text -eq "enabled"){
$mlabel.Text = "disabled"
}
else{
$mlabel.Text = "enabled"
}
}
$global:mbutton = [System.Windows.Forms.Button]::new()
$mbutton.Text = "toggle state"
$mbutton.Location = [System.Drawing.Point]::new(100,100)
# $mbutton.Add_Click( {Button_Click} ) # pressing button shows errors on console
$global:mlabel = [System.Windows.Forms.Label]::new()
$mlabel.Name = "status"
$mlabel.Text = "enabled"
$global:Form = New-Object System.Windows.Forms.Form
$Form.Controls.Add($mlabel)
$Form.Controls.Add($mbutton)
$mbutton.Add_Click( {Button_Click} ) # pressing button does nothing
$Form.ShowDialog() | Out-Null
通常,最好将显示表单作为最后一个操作,因为您需要定义它使用的所有内容,但在任何情况下,在显示表单之前都需要定义一个单击事件…我现在无法访问计算机进行测试,但这可能是一个范围问题-您在一个位置和一个位置引用了“$global:form”在其他文件中使用“$Form”。尝试始终使用“$global:Form”。谢谢。投票通过并接受。它在没有$global:
的情况下也可以使用。除了$Form.ShowDialog()| Out Null
和$mbutton]的错误顺序之外,主要问题似乎都是由于使用了父项
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
Function Button_Click() {
if ($mlabel.Text -eq "enabled"){
$mlabel.Text = "disabled"
}
else{
$mlabel.Text = "enabled"
}
}
$global:mbutton = [System.Windows.Forms.Button]::new()
$mbutton.Text = "toggle state"
$mbutton.Location = [System.Drawing.Point]::new(100,100)
# $mbutton.Add_Click( {Button_Click} ) # pressing button shows errors on console
$global:mlabel = [System.Windows.Forms.Label]::new()
$mlabel.Name = "status"
$mlabel.Text = "enabled"
$global:Form = New-Object System.Windows.Forms.Form
$Form.Controls.Add($mlabel)
$Form.Controls.Add($mbutton)
$mbutton.Add_Click( {Button_Click} ) # pressing button does nothing
$Form.ShowDialog() | Out-Null