how to change the color of the item when the checkbox is unchecked in c#.net?

17,337

Solution 1

item.checked is true if an item is checked, and false if an item is unchecked.

So you can do something like:

if(item.checked)
{
    //Set color
}
else
{
    //Set color of item for unchecked
}

Solution 2

I suppose you are looking for something like this:

private void checkBox1_CheckedChanged(object sender, EventArgs e) {
    if (checkBox1.Checked)
        checkBox1.ForeColor = Color.Green;
    else
        checkBox1.ForeColor = Color.Red;
}

As you might know, the Checked property of the CheckBox control is a boolean. So testing for checkBox1.Checked results in a changes if Checked == true, and !checkBox1.Checked (or an else block) results in changes if Checked == false

Solution 3

control.Color = checkBox.Checked ? Color.Red : Color.Blue;
Share:
17,337
zoya
Author by

zoya

Updated on June 17, 2022

Comments

  • zoya
    zoya almost 2 years

    i want to change the forecolor of the checked item when it is unchecked.. for checked item i have used item.checked but what to do if it is unchecked? im using winforms

  • gangus
    gangus over 4 years
    CheckBox doesn't have Color property