views:

635

answers:

4

The default behavior of property BackColor of a TextBox is as follows: when enabled, it is White (SystemColors.Window), when disabled it is Gray (not sure what SystemColor this is).

If I change the BackColor property, the same color is used for both Enabled and Disabled. How do I reset the BackColor property (after it has been changed previously) so that the behavior reverts to the default?

I have tried setting it back to SystemColors.Window, but then the box stays white when disabled.

+1  A: 

In case there is no way to reset the control so it will return to automatically changing its background color when enabled/disabled I would recommend using the UIElement.IsEnabledChanged event to set your desired enabled/disabled background colors. I hope this is helpful!

Adam Alexander
I think your answer may be talking about when using WPF. His question is directed at winforms (based on the tags)
dustyburwell
+1 Thanks Adam, that's pretty much what I was doing, I went with another solution posted here as it will most likely work on non-standard color schemes.
Patrick McDonald
A: 

SystemColors.Window is (usually) white. Use SystemColors.Control.

You could, of course, cache the color in use when you set it to your own, then use that to reset it. In the end, though, either approach will work.

Adam Robinson
Caching the color is always a good idea :) so that you can return it to it's previous state. Although its a difficult solution when your say, rotating between 5 or 6 colors.
kralco626
+1  A: 
TextBox.ResetBackColor()

It doesn't pop up in IntelliSense for some reason, but it's there and it compiles.

Alex Ruse
I would probably avoid this, as the "Reset[PropertyName]" functions are a part of the serialization framework. Unless there's a compelling reason, I would avoid using functions that are intentionally excluded from Intellisense.
Adam Robinson
+1 for this, I went with @ascalonx's solution though for the reason @Adam Robinson mentioned (At least your solution worked, not sure what drugs Adam is on) ;)
Patrick McDonald
+7  A: 

If you mean in the designer, you can just click into the BackColor property and delete whatever's in there and then press enter. This will reset the property back to it's default value.

If you mean in code, you can set the BackColor property to Color.Empty, and this will have the same effect.

dustyburwell
Works a charm, thanks!
Patrick McDonald