如何防止控件的背景色发生更改?

发布于 2025-01-05 17:10:43 字数 963 浏览 5 评论 0原文

我希望从基础面板继承的面板无论在何处使用都有固定的 BackColor 。我的基本面板如下所示:

public class MyPanel
{
    public override Color BackColor
    {
        get
        {
            return base.BackColor;
        }
        set
        {
            base.BackColor = Color.Red;
        }
    }
}

BackColor 未在示例表单的 Designer.cs 文件中设置:

this.sampleControl.Font = new System.Drawing.Font("Tahoma", 8.25F,
    System.Drawing.FontStyle.Regular, 
    System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.sampleControl.Location = new System.Drawing.Point(0, 0);
this.sampleControl.Margin = new System.Windows.Forms.Padding(5);
this.sampleControl.Name = "sampleControl";
this.sampleControl.Padding = new System.Windows.Forms.Padding(2, 0, 2, 2);
this.sampleControl.Size = new System.Drawing.Size(230, 100);
this.sampleControl.TabIndex = 1;

事实上,任何地方都没有设置颜色,所以我想它以某种方式从它所在的面板获取属性。我怎样才能防止这种情况发生?

I want a panel inheriting from a base panel to have a fixed BackColor no matter where it is used. My base panel looks like this:

public class MyPanel
{
    public override Color BackColor
    {
        get
        {
            return base.BackColor;
        }
        set
        {
            base.BackColor = Color.Red;
        }
    }
}

The BackColor is not set in the Designer.cs file of an example form:

this.sampleControl.Font = new System.Drawing.Font("Tahoma", 8.25F,
    System.Drawing.FontStyle.Regular, 
    System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.sampleControl.Location = new System.Drawing.Point(0, 0);
this.sampleControl.Margin = new System.Windows.Forms.Padding(5);
this.sampleControl.Name = "sampleControl";
this.sampleControl.Padding = new System.Windows.Forms.Padding(2, 0, 2, 2);
this.sampleControl.Size = new System.Drawing.Size(230, 100);
this.sampleControl.TabIndex = 1;

In fact there is no color set anywhere, so I suppose it somehow gets the property from the panel it is placed in. How can I prevent this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤寂小茶 2025-01-12 17:10:43

怎么样:

public class MyPanel : Panel
{
    private Color backColor = Color.Red;

    public MyPanel()
    {
        // Set the color once
        this.BackColor = backColor;
    }

    public override Color BackColor
    {
        get
        {
            return base.BackColor;
        }
        set
        {
            base.BackColor = backColor;
        }
    }
}

How about:

public class MyPanel : Panel
{
    private Color backColor = Color.Red;

    public MyPanel()
    {
        // Set the color once
        this.BackColor = backColor;
    }

    public override Color BackColor
    {
        get
        {
            return base.BackColor;
        }
        set
        {
            base.BackColor = backColor;
        }
    }
}
陌若浮生 2025-01-12 17:10:43

只需在 MyPanel 构造函数中进行设置即可。

BackColor=Color.Red;

您不需要覆盖,除非您想阻止其他人更改它。

Just set that in the MyPanel constructor.

BackColor=Color.Red;

You don't need the override unless you want to prevent others from changing it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文