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

前端 未结 3 1773
挽巷
挽巷 2021-01-14 11:45

I\'m trying to learn and grasp what and how C# does things. I\'m historically a Visual Foxpro (VFP) developer, and somewhat spoiled at the years of visual inheritance by cr

相关标签:
3条回答
  • 2021-01-14 11:49

    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.

    0 讨论(0)
  • 2021-01-14 11:55

    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".

    0 讨论(0)
  • 2021-01-14 12:09

    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.

    0 讨论(0)
提交回复
热议问题