-1

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...

  • can you pase a screenshot of the control/property you are trying to modify? – jmvcollaborator May 19 '23 at 16:40
  • 1
    https://stackoverflow.com/a/20838280/17034 – Hans Passant May 19 '23 at 17:32
  • Here example: [link](https://www.plixo.com.sg/(internals)/CustomPropertiesDefaultValuesInDesigner.png) And following tips here, I found a way: [link](https://learn.microsoft.com/en-us/dotnet/desktop/winforms/controls/defining-default-values-with-the-shouldserialize-and-reset-methods?view=netframeworkdesktop-4.8) Thanks to all for the directions! – Alexis Martial May 19 '23 at 18:05

2 Answers2

0

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

jmvcollaborator
  • 2,141
  • 1
  • 6
  • 17
0

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