繁体   English   中英

如何在代码执行期间禁用 Powershell GUI 按钮?

[英]How to disable a Powershell GUI button during code execution?

在 function 操作期间,我无法禁用按钮。 如果我在单击按钮时禁用它,在视觉上它看起来被禁用但它仍然有效(或者更确切地说它会将点击排队)。

这是我正在测试它的工作演示代码:

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

$Form                            = New-Object system.Windows.Forms.Form
$Form.ClientSize                 = '400,400'
$Form.text                       = "Form"
$Form.TopMost                    = $false
$Button1                         = New-Object system.Windows.Forms.Button
$Button1.text                    = "button"
$Button1.width                   = 100
$Button1.height                  = 25
$Button1.enabled                 = $true
$Button1.location                = New-Object System.Drawing.Point(214,59)
$Button1.Font                    = 'Microsoft Sans Serif,10'

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

Function Test {
    Write-Host "Clicked"
    $Button1.Enabled = $false
    Start-Sleep -Seconds 2
    $Button1.Enabled = $true
}

$Button1.add_Click(
    {
        Test
    }
)
[void]$Form.ShowDialog()

我尝试在 function 中放入[System.Windows.Forms.Application]::DoEvents()button1.Update()

我尝试在初始 function 中禁用按钮,然后通过单击按钮在单独的 function 中调用需要更长时间的部分,但这也不起作用。 IE:

Function DisableBtn1 {
    $Button3.Enabled = $false
}

Function DoStuff {
    Write-Host "Bt1 Clicked"
    Start-Sleep -Seconds 2
    $Button3.Enabled = $True

}

$Button1.add_Click(
    {
        DisableBtn3
        DoStuff
    }
)

在脚本运行时正确禁用 GUI 元素方面是否有明显的遗漏?

继续我上面的评论...

做这个简单的测试将说明我的意思:

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

$Form                            = New-Object system.Windows.Forms.Form
$Form.ClientSize                 = '400,400'
$Form.text                       = "Form"
$Form.TopMost                    = $false
$Button1                         = New-Object system.Windows.Forms.Button
$Button1.text                    = "button"
$Button1.width                   = 100
$Button1.height                  = 25
$Button1.enabled                 = $true
$Button1.location                = New-Object System.Drawing.Point(214,59)
$Button1.Font                    = 'Microsoft Sans Serif,10'

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

Function Test 
{
    Write-Host 'Clicked'
    $Button1.Enabled = $false
    $Button1.IsAccessible = $false
    Start-Sleep -Seconds 2
    $Button1.Enabled = $true
}

$Button1.add_Click(
    {
        Test
    }
)
[void]$Form.ShowDialog()

只是不要使用启用线

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

$Form                            = New-Object system.Windows.Forms.Form
$Form.ClientSize                 = '400,400'
$Form.text                       = "Form"
$Form.TopMost                    = $false
$Button1                         = New-Object system.Windows.Forms.Button
$Button1.text                    = "button"
$Button1.width                   = 100
$Button1.height                  = 25
$Button1.enabled                 = $true
$Button1.location                = New-Object System.Drawing.Point(214,59)
$Button1.Font                    = 'Microsoft Sans Serif,10'

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

Function Test 
{
    Write-Host 'Clicked'
    $Button1.Enabled = $false
    Start-Sleep -Seconds 2
}

$Button1.add_Click(
    {
        Test
    }
)
[void]$Form.ShowDialog()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM