4

I am creating a custom control with a black background but have some issues with the designer. Truth to be told I have a base control class that inherits from UserControl and then some subclasses that represent the final controls that I will use in my GUI. In that base class I override the BackColor property, add the DefaultValue attribute and set the default value to BackColor in the constructor. As an example my code looks something like this:

public partial class MyControl1 : UserControl
{
    public MyControl1()
    {
        InitializeComponent();
        BackColor = Color.Black;            
    }

    [DefaultValue(typeof(Color),"Black")]
    public override Color BackColor
    {
        get
        {
            return base.BackColor;
        }
        set
        {
            base.BackColor = value;
        }
    }
}

...

public partial class MyControl2 : MyControl1
{
    public MyControl2()
    {
        InitializeComponent();
    }
}

The thing is every time I open the designer for MyControl2, BackColor in the properties dialog reverts to System.Drawing.SystemColors.Control and my control is painted grey. If I invoke Reset on BackColor it properly returns to Color.Black, though. Also, the designer doesn't serialize the change to System.Drawing.SystemColors.Control until I make another change to the control.

So, what did I try?

  • I thought it could be related to BackColor being an ambient property so I tried adding the attribute AmbientValue(false). Of course it didn't work.

  • I tried erasing the overridden property, leaving only BackColor=Color.Black in the constructor. Surprisingly it fixed the problem with the designer but now resetting the property reverted it to a default value of System.Drawing.SystemColors.Control. Overriding ResetBackColor() didn't solve this last problem.

By the way, I am working under Visual Studio 2010 and my project was created as a .NET 2.0 Windows Forms Application.

I would be glad whether anyone could help me to find whatever is wrong in my code. It is not something that would prevent me from finishing the project but it is pretty annoying. Thank you very much in advance!

Anarion
  • 41
  • 1
  • 2
  • Is the issue only present in the designer? I.E. when you run an application using your control does it behave as expected? – Sacrilege Feb 11 '14 at 20:46
  • Yes it behaves properly until the designer serializes the change to BackColor which happens only after I make a change to another property of the control. That is one reason why it is so annoying – Anarion Feb 11 '14 at 21:16

1 Answers1

1

This may help - there appears to be some voodoo in the winforms designer (a bit like the XML serializer) that will look for properties which are named a specific way because the DefaultValue doesn't work as you might expect:

The following is an example from another post, I know you are not subclassing a DataGridView, but the principle ought to be the same.

public class MyGridView : DataGridView {
    public MyGridView() {
        this.BackgroundColor = DefaultBackgroundColor;
    }
    public new Color BackgroundColor {
        get { return base.BackgroundColor; }
        set { base.BackgroundColor = value;  }
    }
    private bool ShouldSerializeBackgroundColor() {
        return !this.BackgroundColor.Equals(DefaultBackgroundColor);
    }
     private void ResetBackgroundColor() {
        this.BackgroundColor = DefaultBackgroundColor;
    }
    private static Color DefaultBackgroundColor {
        get { return Color.Red; }
    }
}

Incidently - this isn't my code - it's some more pure genius from Hans Passant... link to original with a full explanation: https://stackoverflow.com/a/20838280/685341

Community
  • 1
  • 1
Jay
  • 9,561
  • 7
  • 51
  • 72
  • Thanks for your reply. That seems to be the most complete way to control serialization and it shouldn't be necessary but if things behaved like they should there wouldn't be any problem to begin with. I'll try it later as a work-around and tell you whether it helped or not – Anarion Feb 11 '14 at 21:22
  • 1
    I tried the code as it is and it didn't work. I will dig deeper in the article you mentioned in your post and test some variants. I am sorry it takes me so long to answer but I just stumbled on some other issues that have higher priority... – Anarion Feb 17 '14 at 01:07