How can “default” values in overridden WinForm controls be prevented?

↘锁芯ラ 提交于 2019-12-01 08:37:31

Here is a simple solution using the ReadOnlyAttribute in the derived class.

class MyLabel : Label
{
    [ReadOnly(true)]
    public override Font Font
    {
        get { return new Font(FontFamily.GenericMonospace, 10); }
        set { /* do nothing */ }
    }
}

The ReadOnlyAttribute will disable property editing in the VS.NET designer.

Instead of using the "ReadOnlyAttribute", try using the "DefaultValueAttribute" instead. If I remember correctly, the designer shouldn't create code to set the property if the current value matches the value stored in the "DefaultValueAttribute".

Try adding the ReadOnly attribute to the properties of your derived classes:

[ReadOnly(true)]
public override Font Font
{
    get{ // Your Implementation Here }
    set{ // Don't really care,do you? }
}

The ReadOnlyAttribute should enforce the ReadOnly behavior at design time.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!