Click here to Skip to main content
16,011,447 members
Home / Discussions / C#
   

C#

 
GeneralRe: Creating Installation Package for a C# Application with web reference Pin
koosala14-May-04 0:31
koosala14-May-04 0:31 
GeneralRe: Creating Installation Package for a C# Application with web reference Pin
Heath Stewart13-May-04 2:00
protectorHeath Stewart13-May-04 2:00 
GeneralRe: Creating Installation Package for a C# Application with web reference Pin
Anonymous18-May-04 4:35
Anonymous18-May-04 4:35 
GeneralThread problem Pin
michael.wikstrom13-May-04 1:08
michael.wikstrom13-May-04 1:08 
GeneralUsercontrol Problem Pin
Anfernius12-May-04 23:43
Anfernius12-May-04 23:43 
GeneralRe: Usercontrol Problem Pin
Aryadip13-May-04 0:10
Aryadip13-May-04 0:10 
GeneralRe: Usercontrol Problem Pin
Anfernius13-May-04 0:55
Anfernius13-May-04 0:55 
GeneralRe: Usercontrol Problem Pin
Heath Stewart13-May-04 3:15
protectorHeath Stewart13-May-04 3:15 
The DefaultValueAttribute takes a value of the property Type. "Color.Green" is a string - BackColor is a property.

Since Color.Green is not a constant value, you can not use it in the DefaultValueAttribute. Instead, set the BackColor of your control (which you really don't need to attribute like you are, or even override it) in your constructor or something.

To make it work like it had a default value, define methods called ShouldSerializeBackColor and ResetBackColor like so:
public class MyControl : Control
{
  public static readonly Color DefaultBackColor = Color.Green;
  public MyControl()
  {
    BackColor = DefaultBackColor;
  }
  protected bool ShouldSerializeBackColor()
  {
    return BackColor != DefaultBackColor;
  }
  protected void ResetBackColor()
  {
    BackColor = DefaultBackColor;
  }
}
The designer uses these two methods via Reflection (seemingly) in a similar fashion as using a DefaultValueAttribute.

One other option is to use the DefaultValueAttribute overload which takes a Type and string like so:
public class MyControl : Control
{
  public static readonly Color DefaultBackColor = Color.Green;
  public MyControl()
  {
    BackColor = DefaultBackColor;
  }
  [DefaultValue(typeof(Color), "Green")]
  public override BackColor
  {
    get { return base.BackColor; }
    set { base.BackColor = value; }
  }
}
The latter example you may find easier, but only works for Types that have a TypeConverter associated with them. Use whichever you prefer. In either case, though, you must still set the default BackColor during initialization.

 

Microsoft MVP, Visual C#
My Articles
GeneralRe: Usercontrol Problem Pin
Anfernius13-May-04 6:19
Anfernius13-May-04 6:19 
GeneralRe: Usercontrol Problem Pin
Heath Stewart13-May-04 3:05
protectorHeath Stewart13-May-04 3:05 
GeneralDragDrop registration failed Pin
michael.wikstrom12-May-04 22:32
michael.wikstrom12-May-04 22:32 
GeneralRe: DragDrop registration failed Pin
Heath Stewart13-May-04 3:03
protectorHeath Stewart13-May-04 3:03 
GeneralGDI+ Image from Graphics Pin
mhmoud rawas12-May-04 22:10
mhmoud rawas12-May-04 22:10 
GeneralRe: GDI+ Image from Graphics Pin
sprout7412-May-04 22:31
sprout7412-May-04 22:31 
GeneralRe: GDI+ Image from Graphics Pin
mhmoud rawas12-May-04 23:35
mhmoud rawas12-May-04 23:35 
GeneralRe: GDI+ Image from Graphics Pin
Heath Stewart13-May-04 3:01
protectorHeath Stewart13-May-04 3:01 
GeneralRe: GDI+ Image from Graphics Pin
mhmoud rawas13-May-04 4:19
mhmoud rawas13-May-04 4:19 
GeneralRe: GDI+ Image from Graphics Pin
Heath Stewart13-May-04 4:50
protectorHeath Stewart13-May-04 4:50 
GeneralRe: GDI+ Image from Graphics Pin
mhmoud rawas14-May-04 19:45
mhmoud rawas14-May-04 19:45 
GeneralRe: GDI+ Image from Graphics Pin
Heath Stewart16-May-04 11:17
protectorHeath Stewart16-May-04 11:17 
Generalregd richtextbox and datagrid Pin
karteek12-May-04 21:47
karteek12-May-04 21:47 
GeneralRe: regd richtextbox and datagrid Pin
Heath Stewart13-May-04 2:58
protectorHeath Stewart13-May-04 2:58 
Generalfirewall Pin
shahrokh nabavi12-May-04 21:07
shahrokh nabavi12-May-04 21:07 
GeneralRe: firewall Pin
Colin Angus Mackay12-May-04 23:26
Colin Angus Mackay12-May-04 23:26 
GeneralRe: firewall Pin
walker_net23-Jun-04 22:54
walker_net23-Jun-04 22:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.