Winforms PowerShell WinForm:如果选中一个或多个复选框,则启用按钮,否则禁用按钮

Winforms PowerShell WinForm:如果选中一个或多个复选框,则启用按钮,否则禁用按钮,winforms,powershell,Winforms,Powershell,应该很直截了当,但无法理解逻辑。下面是我目前拥有的代码,它可以工作,但唯一的问题是,如果选中了多个复选框,并且您取消选中了一个按钮,则该按钮将被禁用,只要选中了一个或多个复选框,我就需要该按钮保持启用状态。我也尝试过各种复杂的If和Elseif语句,但我没有尝试改变这种行为 Add-Type -AssemblyName System.Windows.Forms [System.Windows.Forms.Application]::EnableVisualStyles() $Form

应该很直截了当,但无法理解逻辑。下面是我目前拥有的代码,它可以工作,但唯一的问题是,如果选中了多个复选框,并且您取消选中了一个按钮,则该按钮将被禁用,只要选中了一个或多个复选框,我就需要该按钮保持启用状态。我也尝试过各种复杂的If和Elseif语句,但我没有尝试改变这种行为

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

$Form                            = New-Object system.Windows.Forms.Form
$Form.ClientSize                 = '170,191'
$Form.text                       = "Scheduler"
$Form.TopMost                    = $false

$MonCheckBox                     = New-Object system.Windows.Forms.CheckBox
$MonCheckBox.text                = "Monday"
$MonCheckBox.AutoSize            = $false
$MonCheckBox.width               = 95
$MonCheckBox.height              = 20
$MonCheckBox.location            = New-Object System.Drawing.Point(30,30)
$MonCheckBox.Font                = 'Microsoft Sans Serif,10'

$TueCheckBox                     = New-Object system.Windows.Forms.CheckBox
$TueCheckBox.text                = "Tuesday"
$TueCheckBox.AutoSize            = $false
$TueCheckBox.width               = 95
$TueCheckBox.height              = 20
$TueCheckBox.location            = New-Object System.Drawing.Point(30,50)
$TueCheckBox.Font                = 'Microsoft Sans Serif,10'

$WedCheckBox                     = New-Object system.Windows.Forms.CheckBox
$WedCheckBox.text                = "Wednesday"
$WedCheckBox.AutoSize            = $false
$WedCheckBox.width               = 95
$WedCheckBox.height              = 20
$WedCheckBox.location            = New-Object System.Drawing.Point(30,70)
$WedCheckBox.Font                = 'Microsoft Sans Serif,10'

$ThuCheckBox                     = New-Object system.Windows.Forms.CheckBox
$ThuCheckBox.text                = "Thursday"
$ThuCheckBox.AutoSize            = $false
$ThuCheckBox.width               = 95
$ThuCheckBox.height              = 20
$ThuCheckBox.location            = New-Object System.Drawing.Point(30,90)
$ThuCheckBox.Font                = 'Microsoft Sans Serif,10'

$FriCheckBox                     = New-Object system.Windows.Forms.CheckBox
$FriCheckBox.text                = "Friday"
$FriCheckBox.AutoSize            = $false
$FriCheckBox.width               = 95
$FriCheckBox.height              = 20
$FriCheckBox.location            = New-Object System.Drawing.Point(30,110)
$FriCheckBox.Font                = 'Microsoft Sans Serif,10'

$SchedButton                     = New-Object system.Windows.Forms.Button
$SchedButton.text                = "Schedule"
$SchedButton.width               = 60
$SchedButton.height              = 30
$SchedButton.location            = New-Object System.Drawing.Point(30,134)
$SchedButton.Font                = 'Microsoft Sans Serif,10'
$SchedButton.Autosize            = $true
$SchedButton.Enabled             = $false

$Form.controls.AddRange(@($MonCheckBox,$TueCheckBox,$WedCheckBox,$ThuCheckBox,$FriCheckBox,$SchedButton))

$MonCheckBox.add_CheckedChanged({$SchedButton.Enabled = $MonCheckBox.Checked})
$TueCheckBox.add_CheckedChanged({$SchedButton.Enabled = $TueCheckBox.Checked})
$WedCheckBox.add_CheckedChanged({$SchedButton.Enabled = $WedCheckBox.Checked})
$ThuCheckBox.add_CheckedChanged({$SchedButton.Enabled = $ThuCheckBox.Checked})
$FriCheckBox.add_CheckedChanged({$SchedButton.Enabled = $FriCheckBox.Checked})

[void]$Form.ShowDialog()

我会这样做:

$Form.controls.AddRange(@($MonCheckBox, $TueCheckBox, $WedCheckBox, $ThuCheckBox, $FriCheckBox, $SchedButton))

Function Test-AnyButtonChecked {
    if (
        $MonCheckBox.Checked -or
        $TueCheckBox.Checked -or
        $WedCheckBox.Checked -or
        $ThuCheckBox.Checked -or
        $FriCheckBox.Checked
    ) {
        $SchedButton.Enabled = $true
    }
    else {
        $SchedButton.Enabled = $false
    }
}

$MonCheckBox.add_CheckedChanged( { Test-AnyButtonChecked })
$TueCheckBox.add_CheckedChanged( { Test-AnyButtonChecked })
$WedCheckBox.add_CheckedChanged( { Test-AnyButtonChecked })
$ThuCheckBox.add_CheckedChanged( { Test-AnyButtonChecked })
$FriCheckBox.add_CheckedChanged( { Test-AnyButtonChecked })

[void]$Form.ShowDialog()