lundi 27 mai 2019

Powershell: CheckBox.Add_CheckStateChanged

I've started with powershell forms and this is my first attempt. I'd like to add more checkboxes and I've a problem with "CheckStateChanged". How can I shorten the code with my checkboxes using foreach x in y?

CheckBox1.Add_CheckStateChanged({ if (CheckBox1.Checked){foreach CheckBox in $_.Enabled = $false} else {$CheckBox2.Enabled = true}

My first attempt:

$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}
})




Aucun commentaire:

Enregistrer un commentaire