2

In the Visual Studio Designer, I can change, say, the background of a textbox to blue.

To change it back to its 'default' value, I can right click on that property and click "Default Value".

My question: How do I do that programmatically? I'd like the opportunity to set my controls to and from a custom color back to their windows default (without specifically knowing what they are).

Thanks!

kaiz.net
  • 1,984
  • 3
  • 23
  • 31
greggorob64
  • 2,487
  • 2
  • 27
  • 55
  • 2
    I don't think there's a generic way of doing that. You could produce a table of control types and their default background colors. It's typically either Control (buttons) or Window (textbox). – Steven Hunt Sep 21 '12 at 18:06
  • Unfortunately `BackColor` doesn't have a `DefaultValue` attribute as many of the other properties do have. I believe having a dictionary as @Steven suggests is reasonable. You might want to look at the [SystemColors](http://msdn.microsoft.com/en-us/library/system.drawing.systemcolors.aspx) class. As a start, the default background for textbox is `Window` I believe, and `WindowText` for the foreground. – Patrick Sep 22 '12 at 10:39

2 Answers2

1

Marc Gravell's answer seems to do what you are looking for:

PropertyDescriptor prop = TypeDescriptor.GetProperties(textBox1)["BackColor"];
if (prop.CanResetValue(textBox1)) {
  prop.ResetValue(textBox1);
}
Community
  • 1
  • 1
LarsTech
  • 80,625
  • 14
  • 153
  • 225
0

yes you can change any control's property or also you can see how it build your form via your designer class check your form1.designer.cs and read it you can find every control's initialization there.

and if you want to change ur textbox back color here is a code

   textBox1.BackColor = System.Drawing.SystemColors.HotTrack;
Anant Dabhi
  • 10,864
  • 3
  • 31
  • 49
  • That wont work if the color isn't initially set (windows default value) – greggorob64 Sep 21 '12 at 18:18
  • there is no any default back color if its not set then by default take form background color when you set `textbox.BackColor` its override color – Anant Dabhi Sep 21 '12 at 18:25
  • and its a come from `abstract class TextBoxBase` . – Anant Dabhi Sep 21 '12 at 18:28
  • As a for instance, if you drag a TextBox onto the winforms designer. It has a white backcolor, and black forecolor. If I change both of those, I'd like to get both of those values again. – greggorob64 Sep 21 '12 at 18:38