Powershell Add_CheckStateChanged在一个复选框数组中

时间:2016-12-06 13:23:48

标签: arrays powershell checkbox

在我的表单的标签中,我显示了与我已安排具有特定标签的任务一样多的复选框。下面,我有一个禁用的按钮,只有在选中了至少一个复选框时才启用它。

所以我添加了一个“Add_CheckStateChanged”但它只激活了最后一个复选框的按钮。当我在第一个复选框中检查时,它对按钮没有任何作用。

这是脚本的一部分:

function getTasks($path) {
    $out = @()
    $schedule.GetFolder($path).GetTasks(0) | % {
        $xml = [xml]$_.xml
        $out += New-Object psobject -Property @{
            "Name" = $_.Name
            "Path" = $_.Path
            "LastRunTime" = $_.LastRunTime
            "NextRunTime" = $_.NextRunTime
            "Actions" = ($xml.Task.Actions.Exec | % { "$($_.Command) $($_.Arguments)" }) -join "`n"
        }
    }
    $schedule.GetFolder($path).GetFolders(0) | % {
        $out += getTasks($_.Path)
    }
    $out
}
$tasks = @()
$schedule = New-Object -ComObject "Schedule.Service"
$schedule.Connect() 
$tasks += getTasks("\")
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($schedule) | Out-Null
Remove-Variable schedule
$tasksMySoft = $tasks | Where-Object {$_.Name -match "MySoft1|MySoft2|MySoft3|MySoft4|MySoft5|MySoft6"}
$CheckBoxLabels = $tasksMySoft.name
$CheckBoxCounter = 1

$CheckBoxes = foreach($taskLabel in $CheckBoxLabels) {
    $CheckBox = New-Object System.Windows.Forms.CheckBox
    $CheckBox.DataBindings.DefaultDataSourceUpdateMode = 0  
    $CheckBox.UseVisualStyleBackColor = $True
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Width = 175
    $System_Drawing_Size.Height = 20
    $CheckBox.Size = $System_Drawing_Size
    $CheckBox.TabIndex = 2
    $CheckBox.Text = $taskLabel
    $System_Drawing_Point = New-Object System.Drawing.Point
    if($CheckBoxCounter -lt 9){ 
        $System_Drawing_Point.X = 90}
    elseif($CheckBoxCounter -ge 9){
        $System_Drawing_Point.X = 275}
    if($CheckBoxCounter -lt 9){ 
        $System_Drawing_Point.Y = 45 + (($CheckBoxCounter - 1) * 20)}
    elseif($CheckBoxCounter -ge 9){ 
        $System_Drawing_Point.Y = - 115 + (($CheckBoxCounter - 1) * 20)}
    $CheckBox.Location = $System_Drawing_Point
    $CheckBox.Name = "CheckBox$CheckBoxCounter"
    $CheckBox.Add_CheckStateChanged({
    if($CheckBox.checked -eq $True){
    $GenerateButton.Enabled = $true
    }else{
    $GenerateButton.Enabled = $false}
    })

    $tab.Controls.Add($CheckBox)
    $CheckBox
    $CheckBoxCounter++
}

一些帮助会很好;)

1 个答案:

答案 0 :(得分:0)

我自己找到了。我换了

$CheckBox.Add_CheckStateChanged({
if($CheckBox.checked -eq $True){
$GenerateButton.Enabled = $true
}else{
$GenerateButton.Enabled = $false}
})

通过

$CheckBox.Add_CheckStateChanged({
foreach($CheckBox in $CheckBoxes | Where-Object {$_.checked -eq $false}) {
$GenerateButton.Enabled = $false}
})
$CheckBox.Add_CheckStateChanged({
foreach($CheckBox in $CheckBoxes | Where-Object {$_.checked -eq $true}) {
$GenerateButton.Enabled = $true}
})