How can I set the default color for a user control property browsable and with ColorUI class of ColorEditor?

55 Views Asked by At

How can I set the default color for a user control property browsable and with ColorUI class of ColorEditor?

1/ I tried setting Default value as for native type aka

[DefaultValue(SystemColors.WindowText)]

However this need a constant.

2/ I set a default value in the user control constructor.
This is used by the Designer but it still appears in bold, so can't see it's still default.

What is the trick?
BTW, same for Font properties etc...

2

There are 2 best solutions below

1
jmvcollaborator On

You can create a child class of ApplicationSettingsBase, on the snippet below a sample for a color, you can add a property for fonts,etc:

using System;
using System.Configuration;
using System.Drawing;

public class MyUserSettings : ApplicationSettingsBase
{
    [UserScopedSetting()]
    [DefaultSettingValue("white")]
    public Color BackgroundColor
    {
        get
        {
            return ((Color)this["BackgroundColor"]);
        }
        set
        {
            this["BackgroundColor"] = (Color)value;
        }
    }
}

To use it:

MyUserSettings mus;

private void Form1_Load(object sender, EventArgs e)
{
    mus = new MyUserSettings();
    mus.BackgroundColor = Color.AliceBlue;
    this.DataBindings.Add(new Binding("BackColor", mus, "BackgroundColor"));
}

Further info (including design time binding) here

0
Alexis Martial On

I found the solution.

For such properties, need to define some specially named methods that the Designer will find and use ;-)

Cf defining-default-values-with-the-shouldserialize-and-reset-methods