代码之家  ›  专栏  ›  技术社区  ›  Wakan Tanka

winforms根据标签更改按钮名称

  •  0
  • Wakan Tanka  · 技术社区  · 5 年前

    我的目标是根据显示的文本更改按钮文本 Label

    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()
    
    0 回复  |  直到 5 年前
        1
  •  1
  •   HAL9256    5 年前

    编辑:

    $this.parent.controls

    1. 因为你还没有在表单中添加任何内容。
    2. 因为你在里面 $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()
    
    # ----------------------------------------------
    # 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"
        }
    }