Javed Akram
Javed Akram

Reputation: 15354

Change TextColor of disabled control

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.

Upvotes: 4

Views: 3118

Answers (3)

Hans Olsson
Hans Olsson

Reputation: 55059

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.

Upvotes: 3

Daniel Mošmondor
Daniel Mošmondor

Reputation: 19986

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...

Upvotes: 0

Tim Barrass
Tim Barrass

Reputation: 4939

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

Upvotes: 1

Related Questions