How to set the default value of Colors in a custom control in Winforms?
Asked Answered
C

8

22

I got the value to show up correctly using:

    [DefaultValue ( typeof ( Color ), "255, 0, 0" )]
    public Color LineColor
    {
        get { return lineColor; }
        set { lineColor = value; Invalidate ( ); }
    }

But after I reload the project the control is used, this value is set to White, which I can invoke Reset to get back to Red again, but I don't understand the problem.

How are you supposed to set the default value and make sure it's preserved unless I change the value manually from the default?

Actually I am also doing this, which sets Back and ForeColor to these values and the VS property editor shows them as if they are changed from the default value.

Is this wrong?

    public CoolGroupBox ( )
    {
        InitializeComponent ( );
        base.BackColor = Color.FromArgb ( 5, 5, 5 );
        base.ForeColor = Color.FromArgb ( 0, 0, 0 );
    }
Cucumber answered 5/10, 2009 at 20:27 Comment(0)
T
4

The [DefaultValue(...)] attribute is a hint to designers and code generators. It is NOT an instruction to the compiler.

More info in this KB article.

Turnedon answered 5/10, 2009 at 20:38 Comment(3)
Thanks, I didn't know that. I used a combination of setting the value inside the constructor and the default value, and it shows up correctly now. Except for Red, it has to be Red, if it's 255, 0, 0, VS still thinks it's different than Red.Cucumber
It works, but if you use 255, 0, 0, VS thinks it's different than Red. It shows up as Bold.Cucumber
It is used by reflexion and iti is very useful.Tamarin
A
27

The trick is to use the Hex code of the color:

    [DefaultValue(typeof(Color), "0xFF0000")]
    public Color LineColor
    {
            get { return lineColor; }
            set { lineColor = value; Invalidate ( ); }
    }

I think you can also use "255, 0, 0" but am not sure and have normally used either the named colors or the hex code.

Avast answered 5/10, 2009 at 20:42 Comment(1)
I've just tested, and can confirm that both RGB strings and ARGB strings are valid. Use either "255, 0, 0" or "255, 255, 0, 0" to represent red.Reaganreagen
N
11

What about just setting the private member variable to the default color you want?

private Color lineColor = Color.Red;

public Color LineColor
{
        get { return lineColor; }
        set { lineColor = value; Invalidate ( ); }
}

If you want it preserved, just take out the set accessor.

Edit

I see, you want the property list in the designer to show the default color.

You have to override the BackColor property of the base control, add a new DefaultValueAttribute for your new property, and then actually set the default color in the constructor or in the InitializeComponent() method (in the designer.cs file), which is probably better since this is for the designer.

public partial class RedBackgroundControl : UserControl
{
    public RedBackgroundControl()
    {
        InitializeComponent();
        base.BackColor = Color.Red;
    }

    [DefaultValue(typeof(Color), "Red")]
    new public Color BackColor
    {
        get
        {
            return base.BackColor;
        }
        set
        {
            base.BackColor = value;
        }
    }
}
Nave answered 5/10, 2009 at 20:33 Comment(2)
Thanks, when I did that the value still shows up BOLD, like I changed it from the default. I also added the DefaultValue attribute but that didn't change anything.Cucumber
Also by preserved I meant initially. After that the user can set it to whatever they want.Cucumber
T
4

The [DefaultValue(...)] attribute is a hint to designers and code generators. It is NOT an instruction to the compiler.

More info in this KB article.

Turnedon answered 5/10, 2009 at 20:38 Comment(3)
Thanks, I didn't know that. I used a combination of setting the value inside the constructor and the default value, and it shows up correctly now. Except for Red, it has to be Red, if it's 255, 0, 0, VS still thinks it's different than Red.Cucumber
It works, but if you use 255, 0, 0, VS thinks it's different than Red. It shows up as Bold.Cucumber
It is used by reflexion and iti is very useful.Tamarin
I
0

I didn't have any luck using the DefaultValue attribute with properties of type Color or of type Font, but I did succeed with these methods described in the msdn documentation:

"Defining Default Values with the ShouldSerialize and Reset Methods" http://msdn.microsoft.com/en-us/library/53b8022e(v=vs.90).aspx

I used Color.Empty and null, respectively, as the values for my private backing fields and had the public properties always return something useful.

Intone answered 3/12, 2013 at 15:57 Comment(0)
H
0

You can set specific Attribute in the component.designer.cs in the Initializecomponent Method:

  private void InitializeComponent()
    {
        components = new System.ComponentModel.Container();
        this.LineColor= System.Drawing.Color.FromArgb(255, 0, 0);
    }

Rebuild the project and everything should also show up in the

Hydrodynamics answered 8/8, 2018 at 6:37 Comment(0)
R
0

I used this code and it worked perfectly

Private _BackColorSelect As Color = Color.FromArgb(214, 234, 248)

<DefaultValue(GetType(Color), "214, 234, 248")>
Public Property BackColorSelect As Color
    Get
        Return _BackColorSelect
    End Get
    Set(value As Color)
        _BackColorSelect = value
    End Set
End Property
Roberts answered 2/8, 2022 at 5:1 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Newbill
S
-1

There is quite an article about defaultvalue property initialization on CodeProject

Surfboard answered 22/11, 2012 at 11:17 Comment(1)
Yet it covers nothing about colors in particular.Cockerel
H
-2

In DependencyProperty use a UIPropertyMetadata

like:

public static DependencyProperty TrueColorProperty = DependencyProperty.Register(
        "TrueColor", typeof (Color), typeof (LedControl), new UIPropertyMetadata(Colors.Red));
Hudibrastic answered 17/6, 2013 at 7:49 Comment(1)
The question is about winforms not wpfCockerel

© 2022 - 2025 — McMap. All rights reserved.