[英]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.