16

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Patrick McDonald
  • 64,141
  • 14
  • 108
  • 120

4 Answers4

39

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

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
dustyburwell
  • 5,755
  • 2
  • 27
  • 34
6
TextBox.ResetBackColor()

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Alex Ruse
  • 71
  • 2
  • 1
    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 Apr 17 '09 at 14:18
  • +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 Apr 17 '09 at 14:40
  • @Alex Ruse, This compiled for me, too (also didn't show in Intellisense), but didn't actually work at runtime - the backcolor stayed the color that it was set to elsewhere in the program. – marky Apr 03 '20 at 15:24
3

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
  • 182,639
  • 35
  • 285
  • 343
  • 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 Jul 22 '10 at 17:36
1

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!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Adam Alexander
  • 15,132
  • 5
  • 42
  • 41
  • I think your answer may be talking about when using WPF. His question is directed at winforms (based on the tags) – dustyburwell Apr 17 '09 at 14:18
  • +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 Apr 17 '09 at 14:43