简体   繁体   中英

How to disable a Powershell GUI button during code execution?

I can't get a button to be disabled during a function operation. 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.

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. 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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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