编辑:
$this.parent.controls
-
因为你还没有在表单中添加任何内容。
-
因为你在里面
$MyForm_Load
$this.parent
就像你一样
是
父母。只需删除
.parent
E、 g.试图访问空的内容时抛出错误:
$MyForm_Load = {
$mlabel = [System.Windows.Forms.Label]::new()
$mlabel.Name = "status"
$mlabel.Text = "enabled"
$mbutton = [System.Windows.Forms.Button]::new()
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 )
$this.Controls.Add($mlabel)
$this.Controls.Add($mbutton)
}
之后
和
通过引用
$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)
if ($this.controls["status"].text -eq "enabled"){
$mbutton.text = "disable"
}else{
$mbutton.text = "enable"
}
}