如何防止控件的背景色发生更改?
我希望从基础面板继承的面板无论在何处使用都有固定的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
怎么样:
How about:
只需在 MyPanel 构造函数中进行设置即可。
您不需要
覆盖
,除非您想阻止其他人更改它。Just set that in the MyPanel constructor.
You don't need the
override
unless you want to prevent others from changing it.