按下按钮后winform没有动作 [英] winforms no action after pressing button

查看:40
本文介绍了按下按钮后winform没有动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标是在按下System.Windows.Forms.Button 后更改System.Windows.Forms.Label 的文本.我有以下 OOP 代码.其中 (几乎)有效:

The goal is to change text of System.Windows.Forms.Label after pressing System.Windows.Forms.Button. I have following OOP code. Which (almost) works:

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

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() 
        $mbutton.Text = "toggle state"

        $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()

现在我正在尝试将其重写为不起作用的程序样式:

Now I'm trying to rewrite it to procedural style which is not working:

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"
    }
}

我做错了什么?我该如何调试此类问题?

What I did wrong? How can I debug such issue?

推荐答案

除非绝对需要,否则最好不要使用全局定义.但是在这种情况下,如果您稍微改变一下顺序,它就会起作用:

It's best not to use global definitions unless you absolutely need them. But in this case it WILL work if you change the order of things a little:

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

通常将显示表单作为最后一个操作是一种很好的做法,因为您需要定义它使用的所有内容,但无论如何,在显示表单之前需要定义单击事件...

Generally it's good practice to make showng the form the last action because you need to define everything it uses but in any case, a click event needs to be defined before you show the form...

这篇关于按下按钮后winform没有动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆