Problems with changing the default inverse color in usercontrol

I am creating a user control with a black background, but it has some problems with the designer. In truth, 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 this base class, I override a property BackColor, add an attribute, DefaultValueand set the default value BackColorin 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();
    }
}

, MyControl2, BackColor System.Drawing.SystemColors.Control, . Reset BackColor, , , Color.Black. , System.Drawing.SystemColors.Control, .

, ?

  • , BackColor, , AmbientValue(false). , .

  • , BackColor=Color.Black . , System.Drawing.SystemColors.Control. ResetBackColor() .

, Visual Studio 2010, Windows Forms.NET 2.0.

, - - . , , . !

+4
1

- , winforms ( XML) , , , DefaultValue , :

, , DataGridView, .

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; }
    }
}

- - Hans Passant... : fooobar.com/questions/1526426/... p >

+1

Source: https://habr.com/ru/post/1526424/


All Articles