【发布时间】:2015-07-22 07:30:41
【问题描述】:
我正在尝试编写一个类来覆盖整个项目中的所有标签背景,而不更改它们在 Designer.cs 文件中的定义。
我的意思是我可以简单地将this.label1 = new System.Windows.Forms.Label(); 更改为this.label1 = new myLabel();,但我不想这样做。
这是我的 label.cs
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace T1
{
class Label:System.Windows.Forms.Label
{
public override Color BackColor
{
get
{
return Color.DarkRed;
}
set
{
base.BackColor = value;
}
}
}
}
我试图调用我的命名空间 System.Windows.Forms 以覆盖它当前的标签颜色属性,但到目前为止我无法到达任何地方。 任何帮助,将不胜感激。 万事如意。
【问题讨论】:
-
覆盖标签可能是正确的方法,其次,您可以创建自己的自定义表单类来继承它执行单独的
Initialize步骤,但这也会产生开销 -
@Sayse mmmm,听起来是个不错的选择。您能否提供一个我可以学习的代码示例?非常感谢。
-
首先 - 公开这个类并将名称更改为 MyLabel 或其他东西
-
@MajkeloDev 我把它公开了。但关键是,如果我将类名从
label更改为其他名称,那么我将不得不将this.label1 = new System.Windows.Forms.Label();更改为this.label1 = new myLabel();,例如,这不是我想要实现的目标。 -
@Sam - 我添加了一个答案来演示表单方法,如果这没有任何用处,请告诉我,我将删除答案。
标签: c# oop overriding