0

Using a WinApp form in c#, and many buttons here...

I want to create a condition, that if a button has text in it, then the background color of that button changes. That sounds easy enough to do. But what I have is a common set of buttons, that have text in them dependant of values in a XML document.

Example: Week 1 - Buttons 1, 3 and 5 have text in them. Week 2 - Buttons 2 and 3 have text.

How can I setup a seperate condition to check if the button has text in it or not, and then change the color if there is a text value in the button.

Thank you.

1
  • Let me extend a bit on my aim: In the XML document there is also a field called <Duration>. This might be just 1 button, or 2, or more. So when <Duration> = 2 Buttons I need it to color the first button with the text, and the next button. Commented Nov 20, 2009 at 0:56

2 Answers 2

2

I would extend button and override the label setter such that it also changes the color when setting the contents of the label to some non-empty value.

Sign up to request clarification or add additional context in comments.

Comments

1

Do you want something like this?

foreach (var btn in this.Controls.OfType<Button>()) {
  btn.BackColor = (string.IsNullOrEmpty(btn.Text)) 
                ? SystemColors.ButtonFace : Color.AliceBlue;
}

I would put it in a method, and call it on form load, or whenever the buttons' texts changes.

Comments

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.