4

How to change the color of text, when a control is disabled.I want to set different color when control is disabled in c# winforms.

1
  • 1
    Which type of control? Different controls behave different ways, and you'll need to handle these cases differently. Dec 7, 2010 at 8:44

3 Answers 3

3

Edit: I had made the same mistake as Cody in the comments so corrected my answer.

It depends on which control it is.

For example, if it's a TextBox maybe you could make it ReadOnly instead of disabled. And for some other controls you might be able to do similar things to make them appear disabled without actually being disabled.

However, if you want to do it properly you need to make them Owner-draw or override the OnPaint event and draw the text yourself.

3
  • +1 for suggesting owner-drawn. This is the right way to solve this problem, and it is guaranteed to work for almost all of the common controls. Dec 7, 2010 at 8:52
  • Neither Label, TextBox nor Button support owner-draw. Dec 7, 2010 at 8:58
  • Yeah, my bad. In my head, I was equating owner-draw support with overriding the control's Paint event. I obviously realize this is not exactly the same thing, but it's how you'll need to handle labels, textboxes, and buttons, at minimum. The ControlPaint class can make this a little less painless. Dec 7, 2010 at 9:38
1

You can just do it manually -- when you disable the control, just change the text colour too?

1
  • 3
    If the control in question is a TextBox, this will not work. You can only change its BackColor when it is disabled, not its ForeColor (i.e., the text color). Dec 7, 2010 at 8:44
0

If you have many controls, you can do this:

  • attach your form OnChildAdded event
  • in the event, use if ... is of type structure to determine control type
  • depending on the control type, register proper OnEnabledChange event
  • in the event, change text color appropriately

That way, you will have a piece of code that will work for all your forms and will gradually expand to use all the controls you need.

I'll provide some code if that's the way you want to go...

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.