mardi 6 décembre 2016

Powershell Add_CheckStateChanged in an array of checkboxes

In a tab of my form, I show as much checkbox as I have scheduled tasks with specifics labels. Below, I have a button which is disabled, and I want it enabled only if I have at least one of the checkbox checked.

So I add a "Add_CheckStateChanged" but it activate the button only with the last checkbox. When I check among the firsts checkboxes, it does nothing with the button.

Here is the part of script:

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++
}

Some help would be nice ;)

Aucun commentaire:

Enregistrer un commentaire