简体   繁体   中英

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM