2

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. i.e.:

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?

Weighsone
  • 103
  • 1
  • 10
  • 1
    Does this answer your question? [Disabled button still triggers Click event](https://stackoverflow.com/questions/61808611/disabled-button-still-triggers-click-event) – Theo May 17 '20 at 10:17

1 Answers1

1

Form GUI is a form GUI regardless of the underlying code behind it. There is no such thing as a PowerShell button since PowerShell is not a GUI. Anything happening or being set in a GUI is via the GUI libraries. Why are you disabling the immediately enabling the button in the function, thus making it usable again?

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()
postanote
  • 15,138
  • 2
  • 14
  • 25