When compiling some property values are reset. (Set in the designer)

二次信任 提交于 2019-12-11 07:27:33

问题


I am making a program in C#. For this program I wanted to have some custom Control's added to my main Form.

I made my own progressbar called 'ResourceBar' that overrides ProgressBar. I added some custom properties that can be set in the designer. However, whenever I build these values are reset to a value I do not want.

    #region Special properties
    private Brush fillBrush = new SolidBrush(Color.Black);
    [DefaultValue(typeof(Color), "Black")]
    [RefreshProperties(RefreshProperties.Repaint)]
    [Category("Custom"), Description("Color of the resource.")]
    public Color FillColor
    {
        get
        {
            return (fillBrush as SolidBrush).Color;
        }
        set
        {
            this.fillBrush = new SolidBrush(value);
        }
    }

    private Brush fontBrush = new SolidBrush(Color.Black);
    [DefaultValue(typeof(Color), "Black")]
    [RefreshProperties(RefreshProperties.Repaint)]
    [Category("Custom"), Description("Font Color of the text above the resource bar.")]
    public Color FontColor
    {
        get
        {
            return (fontBrush as SolidBrush).Color;
        }
        set
        {
            this.fontBrush = new SolidBrush(value);
        }
    }

    private bool drawIfNonZero;
    [DefaultValue(true)]
    [RefreshProperties(RefreshProperties.Repaint)]
    [Category("Custom"), Description("If set to true, the ResourceBar is only drawn if maximum is higher than zero.")]
    public bool DrawIfNonZero
    {
        get { return drawIfNonZero; }
        set { drawIfNonZero = value; }
    }

    private bool drawMaximum;
    [DefaultValue(true)]
    [RefreshProperties(RefreshProperties.Repaint)]
    [Category("Custom"), Description("If set to true, the ResourceBar will show the max amount on top of the bar.")]
    public bool DrawMaximum
    {
        get { return drawMaximum; }
        set { drawMaximum = value; }
    }
    private bool fillResourceBar;
    [DefaultValue(true)]
    [RefreshProperties(RefreshProperties.Repaint)]
    [Category("Custom"), Description("If set to true, the ResourceBar will fill the bar according to the value and maximum.")]
    public bool FillResourceBar
    {
        get { return fillResourceBar; }
        set { fillResourceBar = value; }
    }
    #endregion

I didn't have the backing fields before. I added them hoping it would fix my problem, but it did not fix it.


回答1:


@Hans Passant,

Thank you! This was indeed the error. I fixed it by adding '= true'

    [DefaultValue(true)]
    [RefreshProperties(RefreshProperties.Repaint)]
    [Category("Custom"), Description("If set to true, the ResourceBar is only drawn if maximum is higher than zero.")]
    public bool DrawIfNonZero
    {
        get; set;
    }

    [DefaultValue(true)]
    [RefreshProperties(RefreshProperties.Repaint)]
    [Category("Custom"), Description("If set to true, the ResourceBar will show the max amount on top of the bar.")]
    public bool DrawMaximum
    {
        get; set;
    } = true;

    [DefaultValue(true)]
    [RefreshProperties(RefreshProperties.Repaint)]
    [Category("Custom"), Description("If set to true, the ResourceBar will fill the bar according to the value and maximum.")]
    public bool FillResourceBar
    {
        get; set;
    } = true;

Backing fields removed too.



来源:https://stackoverflow.com/questions/30575572/when-compiling-some-property-values-are-reset-set-in-the-designer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!