简体   繁体   中英

Enable/Disable checkBox based on combobox value with ternary operator c#

I'm trying to enable checkbox based on comboxbox selection. I am aware that with simple if condtion can solve my problem but when i change it to ternary operator it shows error

if (ComboBox.SelectedValue.Equals(enum.value))
{
    chkbox.Enabled = false;
}
else 
{ 
    chkbox.Enabled = true; 
}

Ternary:

ComboBox.SelectedValue == enum.value ? chkbox.Enabled = false : chkbox.Enabled = true;

This works fine but when i changed it to ternary it throws errors. How can i change it to make it work

Thanks in Advance.

Your ternary syntax is incorrect. It should be like this instead:

chkbox.Enabled = ComboBox.SelectedValue == enum.value ? false : true;

You could also write it like this:

chkbox.Enabled = ComboBox.SelectedValue != enum.value;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM