How to prevent "defaults" in redefined WinForm controls?

I am trying to find out and understand what and how C # does. I have historically been a Visual Foxpro (VFP) developer and have been a little spoiled during the years of visual inheritance by creating my own basic system of user controls that will be used on a large scale.

In an attempt to learn parallels in C #, I was stuck on something. Let's say I get my own control over labels (the control is a subclass of the label) defined by the font "Arial", 10 points. Then, in any form that I add, the Designer will automatically pre-populate some property values ​​that can be seen in the "Designer.cs" section of the Form class.

this.LabelHdr2.AutoSize = true;
this.LabelHdr2.Font = new System.Drawing.Font("Arial", 14F, System.Drawing.FontStyle.Bold);
this.LabelHdr2.ForeColor = System.Drawing.Color.Blue;
this.LabelHdr2.Location = new System.Drawing.Point(150, 65);
this.LabelHdr2.Name = "LabelHdr2";
this.LabelHdr2.Size = new System.Drawing.Size(158, 22);
this.LabelHdr2.TabIndex = 5;
this.LabelHdr2.Text = "LabelHdr2";

I want objects like Font, Color, Size, AutoSize to be generated every time the control is placed on the form. If later I decide to change the font from "Arial" 10 to "Tahoma" 11, I will need to go back to all forms (and any other custom controls) and edit to change.

In VFP, if I change anything from my base word, all forms will automatically recognize the changes. I don’t need to edit anything (except for possible alignments using the effect of size) ... but color, font and everything else is not a problem in VFP ...

In C #, I need to go back and change each form so that it is recognized by new / updated class values ​​...

Is there any reasonable way to avoid this?

+3
3

, ReadOnlyAttribute .

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

ReadOnlyAttribute VS.NET.

+3

"ReadOnlyAttribute" DefaultValueAttribute. , , , "DefaultValueAttribute".

+4

ReadOnly :

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

ReadOnlyAttribute ReadOnly .

+1

Source: https://habr.com/ru/post/1704627/


All Articles