Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Greying out a checkbox when another is checked

Tags:

c#

Basically, I have a list of delivery checkboxes one for deliver to this address and another for deliver to a separate address, I basically want to make it so once one has been checked the other can not be checked out (perhaps by greying it out or something along those lines)

Please be aware that both boxes use the same controls.

like image 926
Hello World Avatar asked Jun 14 '12 12:06

Hello World


People also ask

How do I make a checkbox greyed out?

You can style checkbox label and readonly inputs with CSS, e.g.: input [readonly="readonly"] {} but the browser should make the checkbox should appear greyed out when set to readonly. "checkbox itself should appear greyed out just by setting the readonly attribute" - Tried in IE8, FF12.

How do I GREY out a checkbox in C#?

Use the following code, checkboxToBeGreyed. Enabled = false; You have write this code in other checkbox's checked event .

Can a checkbox be disabled?

The disabled property sets or returns whether a checkbox should be disabled, or not. A disabled element is unusable and un-clickable. Disabled elements are usually rendered in gray by default in browsers. This property reflects the HTML disabled attribute.

Can a checkbox be checked and disabled?

In checkboxes, for example, you can check them on or off (thus setting the CHECKED state) but you don't change the value of the field.


2 Answers

Listen to the first CheckBox's CheckedChanged event with a method like this one:

private void checkBox1_checkedChanged(object sender, EventArgs e)
{
    this.checkBox2.Enabled = !this.checkBox1.Checked;

    // If you want it to be unchecked as well as grayed out,
    // then have this code as well:
    if (!this.checkBox2.Enabled)
    {
        this.checkBox2.Checked = false;
    }
}

But you should consider using RadioButtons instead of CheckBoxes, if it logically fits to your needs.

like image 171
SimpleVar Avatar answered Nov 07 '22 07:11

SimpleVar


Use the following code,

checkboxToBeGreyed.Enabled = false;

You have write this code in other checkbox's checked event . Hope this helps.

like image 1
Rajesh Subramanian Avatar answered Nov 07 '22 09:11

Rajesh Subramanian