Powershell winforms根据标签更改按钮名称
我的目标是根据Powershell winforms根据标签更改按钮名称,powershell,Powershell,我的目标是根据标签显示的文本更改按钮文本。我有以下代码: Add-Type -AssemblyName System.Windows.Forms class MyForm : System.Windows.Forms.Form { MyForm($mystuff) { #Do-Stuff $this.Add_Load( $this.MyForm_Load ) } $MyForm_Load = { $mlabel = [
标签显示的文本更改按钮文本。我有以下代码:
Add-Type -AssemblyName System.Windows.Forms
class MyForm : System.Windows.Forms.Form {
MyForm($mystuff) {
#Do-Stuff
$this.Add_Load( $this.MyForm_Load )
}
$MyForm_Load = {
$mlabel = [System.Windows.Forms.Label]::new()
$mlabel.Name = "status"
$mlabel.Text = "enabled"
$mbutton = [System.Windows.Forms.Button]::new()
if ($this.parent.controls["status"].text -eq "enabled"){
$mbutton.text = "disable"
}else{
$mbutton.text = "enable"
}
$mbutton.Location = [System.Drawing.Point]::new(100,100)
$mbutton.Add_Click( $this.mbutton_click )
$this.Controls.Add($mlabel)
$this.Controls.Add($mbutton)
}
$mbutton_click = {
if ($this.Parent.Controls["status"].Text -eq "enabled"){
$this.Parent.Controls["status"].Text = "disabled"
}
else{
$this.Parent.Controls["status"].Text = "enabled"
}
}
}
$foo = [MyForm]::new("test")
$foo.ShowDialog()
但此脚本返回以下错误:
Cannot index into a null array.
At C:\Users\wakatana\Desktop\toggle_button_name\forms2.ps1:50 char:13
+ if ($this.parent.controls["status"].text -eq "enabled"){
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArray
编辑-最终工作解决方案:
Add-Type -AssemblyName System.Windows.Forms
class MyForm : System.Windows.Forms.Form {
MyForm($mystuff) {
#Do-Stuff
$this.Add_Load( $this.MyForm_Load )
}
$MyForm_Load = {
$mlabel = [System.Windows.Forms.Label]::new()
$mlabel.Name = "label"
$mlabel.Text = "disabled"
$mbutton = [System.Windows.Forms.Button]::new()
$mbutton.Name = "button"
$mbutton.Location = [System.Drawing.Point]::new(100,100)
$mbutton.Add_Click( $this.mbutton_click )
$this.Controls.Add($mlabel)
$this.Controls.Add($mbutton)
# ----------------------------------------------
# Now $this.controls has something. We can now access it.
# ----------------------------------------------
if ($this.controls["label"].text -eq "enabled"){
$mbutton.text = "disable"
}else{
$mbutton.text = "enable"
}
}
$mbutton_click = {
if ($this.Parent.Controls["label"].Text -eq "enabled"){
$this.Parent.Controls["label"].Text = "disabled"
$this.Parent.Controls["button"].Text = "enable"
}
else{
$this.Parent.Controls["label"].Text = "enabled"
$this.Parent.Controls["button"].Text = "disable"
}
}
}
$foo = [MyForm]::new("test")
$foo.ShowDialog()
编辑:
$this.parent.controls
为空有两个原因
因为您尚未在表单中添加任何内容李>
因为您在$MyForm\u Load
内,所以不需要$this.parent
,因为您是家长。只需删除.parent
,并直接访问控件即可
例如,尝试访问空的内容会抛出错误:
$MyForm_Load = {
$mlabel = [System.Windows.Forms.Label]::new()
$mlabel.Name = "status"
$mlabel.Text = "enabled"
$mbutton = [System.Windows.Forms.Button]::new()
# ----------------------------------------------
# Here - $this.controls is empty trying to access will throw error
# ----------------------------------------------
if ($this.controls["status"].text -eq "enabled"){
$mbutton.text = "disable"
}else{
$mbutton.text = "enable"
}
$mbutton.Location = [System.Drawing.Point]::new(100,100)
$mbutton.Add_Click( $this.mbutton_click )
# ----------------------------------------------
# Here - this.controls is still empty
# ----------------------------------------------
$this.Controls.Add($mlabel)
$this.Controls.Add($mbutton)
# ----------------------------------------------
# Now $this.controls has something.
# ----------------------------------------------
}
因此,通过在添加控件后重新排列脚本以访问该控件,和通过引用$this.controls
,可以获得:
$MyForm_Load = {
$mlabel = [System.Windows.Forms.Label]::new()
$mlabel.Name = "status"
$mlabel.Text = "enabled"
$mbutton = [System.Windows.Forms.Button]::new()
$mbutton.Location = [System.Drawing.Point]::new(100,100)
$mbutton.Add_Click( $this.mbutton_click )
$this.Controls.Add($mlabel)
$this.Controls.Add($mbutton)
# ----------------------------------------------
# Now $this.controls has something. We can now access it.
# ----------------------------------------------
if ($this.controls["status"].text -eq "enabled"){
$mbutton.text = "disable"
}else{
$mbutton.text = "enable"
}
}
谢谢你的回复,但它不适合我,总是一样的错误,没有按钮文本啊!我明白了。首先要添加前面列出的控件,其次,因为MyForm是父控件,所以不需要使用.parent
来引用控件。请参阅上面的编辑。谢谢,但我仍然在$mbutton\u单击我的实际代码下遇到错误:我也有一些与此代码相关的其他问题,但我不认为这可能与此相关。谢谢。更新并接受。以前我犯过错误。我已经删除了Parent
下的$mbutton\u单击。这对我来说仍然很奇怪,为什么它是这样工作的。我提出了另一个问题,关于为什么在一个地方需要Parent
,而在另一个地方不需要。