我有一些带有块的powershell脚本:
$checkBox_1.Add_CheckStateChanged({
if( $checkBox_1.Checked -or
$checkBox_2.Checked -or
$checkBox_3.Checked -or
$checkBox_4.Checked -or
$checkBox_5.Checked -or
$checkBox_6.Checked -or
$checkBox_7.Checked -or
$checkBox_8.Checked -or
$checkBox_9.Checked -or
$checkBox_10.Checked -or
$checkBox_11.Checked -or
$checkBox_12.Checked -or
$checkBox_13.Checked -or
$checkBox_14.Checked ) {
$okButton.Enabled = $True
}
else {
$okButton.Enabled = $False
}
})
$checkBox_2.Add_CheckStateChanged({
if( $checkBox_1.Checked -or
$checkBox_2.Checked -or
$checkBox_3.Checked -or
$checkBox_4.Checked -or
$checkBox_5.Checked -or
$checkBox_6.Checked -or
$checkBox_7.Checked -or
$checkBox_8.Checked -or
$checkBox_9.Checked -or
$checkBox_10.Checked -or
$checkBox_11.Checked -or
$checkBox_12.Checked -or
$checkBox_13.Checked -or
$checkBox_14.Checked ) {
$okButton.Enabled = $True
}
else {
$okButton.Enabled = $False
}
})
和另外12个相似的街区。
是否可以更短,更好的方式编写此块?
答案 0 :(得分:0)
只创建一次所有复选框的数组:
$checkboxes = Get-Variable checkBox_*
$checkboxes | ForEach {
$_.Value.Add_CheckStateChanged({
$okButton.Enabled = $True -in ($checkboxes | ForEach { $_.Value.Checked })
})
}
在PS3.0 +中,内部检查可以简化:
$okButton.Enabled = $True -in $checkboxes.Value.Checked