30

I have created a simple Calendar application and I would like to change the color of names of the days that are displayed. I created a simple condition:

if (nameDay.Text.Equals("Sunday"))
{
    daytxt.Foreground = Brushes.Red;
}

But in this case the color is changing permanently. When the name of day changes to "Monday" then color of the text is still red but it should be black. How can I fix my issue?

1 Answer 1

30

An else condition is missing from your if statement in order to achieve what you need.

You can do it 1 of 2 ways:

if (nameDay.Text.Equals("Sunday"))
{
    daytxt.Foreground = Brushes.Red;
}
else
{
    daytxt.Foreground = Brushes.Black;
}

Or

daytxt.Foreground = nameDay.Text.Equals("Sunday") ? Brushes.Red : Brushes.Black;
Sign up to request clarification or add additional context in comments.

2 Comments

So it enough to add "else"... Thank you, it's working.
Why it should turn black again if you don't tell it somewhere?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.