简体   繁体   English

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

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

I can't get a button to be disabled during a function operation.在 function 操作期间,我无法禁用按钮。 If I disable it when the button is clicked, visually it looks disabled but it still works (or rather it queues the click).如果我在单击按钮时禁用它,在视觉上它看起来被禁用但它仍然有效(或者更确切地说它会将点击排队)。

This is the working demo code I'm testing it on:这是我正在测试它的工作演示代码:

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

I have tried putting in [System.Windows.Forms.Application]::DoEvents() and button1.Update() in the function.我尝试在 function 中放入[System.Windows.Forms.Application]::DoEvents()button1.Update()

I've tried disabling the button in an initial function and then calling the part that takes longer in a separate function from the button click but that didn't work either.我尝试在初始 function 中禁用按钮,然后通过单击按钮在单独的 function 中调用需要更长时间的部分,但这也不起作用。 ie: IE:

Function DisableBtn1 {
    $Button3.Enabled = $false
}

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

}

$Button1.add_Click(
    {
        DisableBtn3
        DoStuff
    }
)

Is there something obvious I'm missing in terms of making a GUI element properly disabled while a script is running?在脚本运行时正确禁用 GUI 元素方面是否有明显的遗漏?

Continued from my comment above...继续我上面的评论...

Doing this simple test will show what I mean:做这个简单的测试将说明我的意思:

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

Just don't use the enable line只是不要使用启用线

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