无法根据值重命名按钮

时间:2019-05-09 07:27:04

标签: powershell

如果变量等于1,则将按钮名称更改为Reset;否则将其更改回原样。但是,我无法使其正常工作。

我尝试设置写主机以显示变量值,并且正确显示。但是,一旦再次按下按钮,变量号就会切换回原来的位置,从而导致一旦按下按钮,按钮文本就不会改变。我也尝试过删除该函数,并直接在按钮代码中构建它。结果相同。

Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()

$Form                            = New-Object system.Windows.Forms.Form
$Form.ClientSize                 = '273,155'
$Form.text                       = "Rename_Test"
$Form.TopMost                    = $false

$BRename                         = New-Object system.Windows.Forms.Button
$BRename.text                    = "Rename"
$BRename.width                   = 182
$BRename.height                  = 88
$BRename.location                = New-Object System.Drawing.Point(39,26)
$BRename.Font                    = 'Microsoft Sans Serif,10'

$Form.controls.AddRange(@($BRename))

#Variables
$MyNumber = [int]1

Function AddNumber{
    $MyNumber = [int]$MyNumber + [int]1
    $BRename.text = "Reset"
}

Function SubNumber{
    $MyNumber = [int]$MyNumber - [int]1
    $BRename.text = "Rename"
}

Function GetNumber {
    $MyNumber
}

$BRename.Add_Click({

    if($MyNumber -eq [int]1){
        AddNumber
    }

    If($MyNumber -eq [int]2){
        SubNumber
    }

 })

 [void]$Form.ShowDialog()

2 个答案:

答案 0 :(得分:2)

原因是可变范围。默认情况下,在函数中更改变量不会在其父范围内更改它。因此

Function SubNumber{
    $MyNumber = [int]$MyNumber - [int]1

将仅在$MyNumber函数内部更改SubNumber,原始的不变。这非常类似于按值传递原理图。结果是$MyNumber从不为2,因此从不调用减法函数。

请注意,在Powershell脚本中,不需要显式[int]也不是典型的

要在脚本范围内更改变量,请在函数内使用$script:前缀。像这样

Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()

$Form                            = New-Object system.Windows.Forms.Form
$Form.ClientSize                 = '273,155'
$Form.text                       = "Rename_Test"
$Form.TopMost                    = $false

$BRename                         = New-Object system.Windows.Forms.Button
$BRename.text                    = "Rename"
$BRename.width                   = 182
$BRename.height                  = 88
$BRename.location                = New-Object System.Drawing.Point(39,26)
$BRename.Font                    = 'Microsoft Sans Serif,10'

$Form.controls.AddRange(@($BRename))

#Variables
$MyNumber = 1

function Renamed {
    switch ($script:MyNumber) { # Switch is usually better than multiple if statements
        1 { $BRename.text = "Reset"; ++$script:MyNumber }
        2 { $BRename.text = "Rename"; --$script:MyNumber }
        default  {$script:MyNumber = 1} # For unexpected values, force inital value
    }
}
$BRename.Add_Click({Renamed})

[void]$Form.ShowDialog()

答案 1 :(得分:0)

查看代码中的注释:

    Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()


# Scriptblock for button pressed

$script:MyNumber = -1

$action = {
    $script:MyNumber = -$script:MyNumber
    if( $script:MyNumber -gt 0 ) {
        $BRename.text = "Reset"
    }
    else {
        $BRename.text = "Rename"
    }
}

$form                            = New-Object system.Windows.Forms.Form
$form.Visible                    = $false
[void]$form.SuspendLayout()

$form.ClientSize                 = '273,155'
$form.text                       = "Rename_Test"
$form.TopMost                    = $false

$BRename                         = New-Object system.Windows.Forms.Button
$BRename.text                    = "Rename"
$BRename.width                   = 182
$BRename.height                  = 88
$BRename.location                = New-Object System.Drawing.Point(39,26)
$BRename.Font                    = 'Microsoft Sans Serif,10'
$BRename.Add_Click( $action )    # event must asigned before showDialog!

$Form.controls.Add($BRename)

[void]$form.ResumeLayout()
$userInput = $form.ShowDialog()