I've started with powershell forms and this is my first attempt.
I'd like to add more checkboxes and I've no problem with "CheckStateChanged". I'm looking for an alternative code with foreach declaration. How can I shorten the code with my checkboxes using foreach x in y?
My first attempt works well:
$CheckBox1.Add_CheckStateChanged({
if ($CheckBox1.Checked){$CheckBox2.Enabled = $false} else {$CheckBox2.Enabled = $true}
if ($CheckBox1.Checked){$CheckBox3.Enabled = $false} else {$CheckBox3.Enabled = $true}
if ($CheckBox1.Checked){$CheckBox4.Enabled = $false} else {$CheckBox4.Enabled = $true}
})
$CheckBox2.Add_CheckStateChanged({
if ($CheckBox2.Checked){$CheckBox1.Enabled = $false} else {$CheckBox1.Enabled = $true}
if ($CheckBox2.Checked){$CheckBox3.Enabled = $false} else {$CheckBox3.Enabled = $true}
if ($CheckBox2.Checked){$CheckBox4.Enabled = $false} else {$CheckBox4.Enabled = $true}
})
$CheckBox3.Add_CheckStateChanged({
if ($CheckBox3.Checked){$CheckBox1.Enabled = $false} else {$CheckBox1.Enabled = $true}
if ($CheckBox3.Checked){$CheckBox2.Enabled = $false} else {$CheckBox2.Enabled = $true}
if ($CheckBox3.Checked){$CheckBox4.Enabled = $false} else {$CheckBox4.Enabled = $true}
})
$CheckBox4.Add_CheckStateChanged({
if ($CheckBox4.Checked){$CheckBox1.Enabled = $false} else {$CheckBox1.Enabled = $true}
if ($CheckBox4.Checked){$CheckBox2.Enabled = $false} else {$CheckBox2.Enabled = $true}
if ($CheckBox4.Checked){$CheckBox3.Enabled = $false} else {$CheckBox3.Enabled = $true}
})
An alternative I'm looking for might look like:
CheckBox1.Add_CheckStateChanged({
if (CheckBox1.Checked){foreach CheckBox in $_.Enabled = $false} else {$CheckBox2.Enabled = true}
Unfortunately I can't figure it out how to use a foreach function with checkboxes. I'd like to add more than ten checkboxes to my form.
CheckStateChanged". It's not clear whether something isn't working or if you just want shorter code. If you're encountering an error or the code isn't doing what you expect please edit the question to add those details.