zoya
zoya

Reputation: 483

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

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

Upvotes: 4

Views: 18962

Answers (3)

Ikke
Ikke

Reputation: 101261

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
}

Upvotes: 2

thelost
thelost

Reputation: 6704

control.Color = checkBox.Checked ? Color.Red : Color.Blue;

Upvotes: 3

Webleeuw
Webleeuw

Reputation: 7282

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

Upvotes: 4

Related Questions